Prototype Pattern in Java (Shallow vs Deep Copy)
The Prototype pattern explained: create new objects by cloning an existing one instead of expensive construction. The shallow-vs-deep-copy gotcha, Java Cloneable, and when to use it. With code and UML.
🟠Design Patterns · Creational · Senior
The Prototype pattern creates new objects by copying an existing one rather than building it from scratch. When making an object is expensive — it took a database call, heavy computation, or lots of configuration to set up — cloning a ready-made "prototype" is far cheaper. It's the least-used of the five creational patterns, but it has one deep gotcha (shallow vs deep copy) that makes it a good interview topic.
clone() method, instead of calling new and re-doing expensive setup.
The problem
Suppose creating an object is costly — say a Document loaded from a database with parsed content and computed layout. If you need many similar copies, re-building each from scratch is wasteful. Or you have a fully-configured object and just want a duplicate you can tweak. Calling new and re-running all that setup each time is the pain Prototype removes.
The solution
🟠Give objects a clone() method that returns a copy of themselves. To get a new object, you ask an existing one to clone:
interface Prototype { Prototype clone(); }
class Document implements Prototype {
private String content;
private List<String> sections;
public Document clone() {
Document copy = new Document();
copy.content = this.content;
copy.sections = new ArrayList<>(this.sections); // deep copy the list!
return copy;
}
}
Document template = loadExpensiveTemplate(); // do the costly setup ONCE
Document d1 = template.clone(); // cheap copies
Document d2 = template.clone();
Now the expensive setup happens once; every new document is a quick copy. In Java, this is often done via the Cloneable interface and Object.clone(), though many prefer a hand-written copy constructor or method for clarity.
The gotcha: shallow vs deep copy
🔴 This is the whole reason Prototype is interesting. A shallow copy copies the object's fields as-is — but if a field is a reference (a list, another object), both the original and the copy end up pointing to the same nested object. Change it through one, and the other sees the change too — a nasty, hard-to-trace bug.
A deep copy recursively copies the nested objects too, so the clone is fully independent. When you implement clone(), you must decide per field: primitives and immutables can be shared safely, but mutable nested objects (lists, maps, custom objects) usually need their own copies. Knowing exactly when a deep copy is required is the senior insight here.
When to use it
🟠Use Prototype when: object creation is expensive and you need many similar instances; you want to duplicate a configured object at runtime; or you want to avoid a rigid hierarchy of factory subclasses. It's common in games (spawn many enemies from one template), graphics editors (copy/paste a shape), and caching pre-built objects. If creation is cheap, just use new.
The interview-ready summary
"Prototype creates objects by cloning an existing instance rather than re-running expensive construction — useful when setup is costly or you want to duplicate a configured object. The key pitfall is shallow vs deep copy: a shallow copy shares nested mutable objects, so edits leak between original and clone; I deep-copy any mutable fields (lists, nested objects) to keep the clone independent. In Java it's Cloneable/clone() or a copy constructor. I'd use it for spawning many similar objects or copy/paste features." Leading with the deep-copy gotcha is exactly what they want."
What to read next
- Adapter Pattern — the first structural pattern
- Builder Pattern — the other way to construct complex objects
- All Design Patterns (hub)
- ← The complete System Design guide (hub)