The Prototype Pattern - Clone, Don't Rebuild
The Prototype Pattern
The Prototype pattern creates new objects by copying an existing object - the prototype - instead of building them from scratch. When construction is expensive or involves many steps, you make one well-configured object and clone it whenever you need another one.
The Problem: Expensive Construction
Imagine a 3D editor that loads a detailed character model from disk, applies textures, and wires up animations. Doing all of that for every new character on screen takes seconds. Re-using one loaded model and copying it takes milliseconds. That is the gap the Prototype solves.
+-----------------------+
| CharacterModel |
| (original prototype) |
+-----------------------+
| | |
> > > clone()
+---------+ +---------+ +---------+
| copy 1 | | copy 2 | | copy 3 |
| (new) | | (new) | | (new) |
+---------+ +---------+ +---------+
Each branch off the original produces an independent copy. Cloning costs a small fraction of what loading costs.
A Java Prototype
Java offers the Cloneable interface, which lets a class declare that it supports cloning.
public class Report implements Cloneable {
private String title;
private String body;
public Report(String title, String body) {
this.title = title;
this.body = body;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public void setBody(String body) { this.body = body; }
public String formatted() {
return this.title + " - " + this.body;
}
}
Walkthrough: Cloneable is a marker interface - it signals intent but declares no methods. The class overrides clone() to call Object.clone(), which performs a field-by-field copy. The constructor still exists, but the clone path bypasses it, so the expensive setup never repeats.
Clone in Action
The caller keeps one master report and clones it per customer.
Report template = new Report("Quarterly Summary", "Base content");
Report a = (Report) template.clone();
a.setBody("Copy for North region");
Report b = (Report) template.clone();
b.setBody("Copy for South region");
Both copies start identical to the template, then diverge independently. The template itself never changes, which is why the pattern is called a prototype: all new objects are variants of one master original.
A Real-World Case
Document editors and game engines use prototypes heavily. A presentation app keeps a slide template with the company logo, fonts, and colour scheme pre-applied. Each new slide clones that template, so every slide inherits branding with zero rebuild. Some languages even skip constructors entirely and make the prototype registry the heart of object creation, with a map from a name to a ready-to-clone instance.
Key Points
- Prototype copies an existing instance instead of constructing a new one.
- It saves time when setup is expensive, such as loading files or applying many defaults.
- clone() must be overridden in every class that needs to be cloned.
- Beware shallow copies: reference fields are shared unless you deep-clone them.
- Prototype pairs well with a registry that stores named, ready-made instances.
- Use it when you need many similar objects that differ only slightly.