Prototype
The prototype pattern creates new objects by copying an existing instance. You use it when constructing from scratch is expensive or when the new object should start as a variant of one you already have.
Detailed Explanation
new plus a long setup (loading a template, connecting a prototype graph) can be wasteful if you need many similar objects. A prototype holds a ready example. Clients call copy() or clone() and then change the few fields that differ.
Java's Object.clone and Cloneable are a poor default. clone is protected, shallow by default, and easy to get wrong with mutable fields. Prefer a copy constructor, a copy factory, or a record (which copies components). For graphs, decide shallow vs deep: a shallow copy shares nested objects; a deep copy walks the graph.
You reach for a prototype when a registry of named templates (standardInvoice.copy()) is the domain language. You reach for a factory when creation does not start from an instance. You reach for deserialization (readObject, JSON) when the prototype lives outside the process.
The cost of clone is a surprising shallow copy that shares a mutable list. The cost of a deep copy of a huge graph is time and memory. The cost of a prototype that still holds a live socket is a copied object that shares the socket.
Immutable objects do not need this pattern: you just share the instance. Prototype is for mutable starting states or for expensive immutable graphs you copy-on-write.
Copyable as your own interface with T copy() is clearer than Cloneable. Records give you new Record(old.a(), old.b()).
Use a prototype registry only when the templates are named and few: "standard invoice", "rush invoice". A map from name to prototype is then a factory that starts from an example. If every copy immediately overwrites every field, you did not need a prototype; you needed a constructor. The copy has to save work or preserve a structure that is hard to rebuild.
Code Examples
import java.util.ArrayList;
import java.util.List;
public final class Invoice {
private final String title;
private final List<String> lines;
public Invoice(String title, List<String> lines) {
this.title = title;
this.lines = new ArrayList<>(lines); // own the list
}
public Invoice copy() {
return new Invoice(title, lines); // constructor copies again
}
public Invoice withTitle(String title) {
return new Invoice(title, lines);
}
public static void main(String[] args) {
Invoice proto = new Invoice("std", List.of("item"));
Invoice next = proto.withTitle("copy");
System.out.println(next.title); // prints: copy
System.out.println(proto.title); // prints: std
}
}Common Gotchas
`clone()` is shallow unless you override carefully
nested lists stay shared.
Prefer copy constructors over `Cloneable`
the interface is marker-only and easy to misuse.
Immutable values can be shared
copying them is wasted work.
A prototype that holds I/O resources will share them after a shallow copy
A prototype that holds I/O resources will share them after a shallow copy