Prototype
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Intent
Create new objects by cloning an existing instance (the prototype) rather than constructing from scratch, allowing you to duplicate complex objects without coupling your code to their concrete classes.
Problem
You need to create an exact copy of an object, but the object may have private fields or complex internal state that is not accessible from outside. Calling 'new' and manually copying every field is brittle, tightly coupled to the concrete class, and breaks when the class changes. Some objects are expensive to set up from scratch when a pre-configured template already exists.
Solution
Declare a common interface (or use a language trait) with a clone method. Each concrete class implements clone by creating a new instance of itself and copying its internal state — including private fields. Clients ask the prototype to clone itself rather than instantiating a new object directly.
Participants
- Prototype — declares the clone interface
- ConcretePrototype — implements the clone operation by copying its own internal state into a new instance
- Client — creates a new object by asking the prototype to clone itself
Advantages
- Clones objects without coupling to their concrete classes — the client only knows the prototype interface
- Eliminates repeated initialization code when preconfigured templates are available
- Produces complex objects more conveniently than building them from scratch
- Provides an alternative to subclassing for varying the type of objects a factory creates
Disadvantages
- Cloning complex objects with circular references or deep dependency graphs can be tricky
- Deep copy vs. shallow copy semantics must be decided and documented carefully
- Languages without a built-in clone mechanism require significant boilerplate
Real-World Analogy
Cell division (mitosis) is nature's prototype pattern. Rather than assembling a new cell from individual molecules, an existing cell duplicates its entire internal state — DNA, organelles, membranes — and splits into two independent copies. Each copy can then mutate independently without affecting the original.
Use Cases
- Duplicating graphical shapes in a drawing editor while preserving position, color, and layer
- Creating document templates that users can clone and customize
- Game development: spawning enemies from a preconfigured prototype rather than re-reading config files
- Caching expensive-to-create objects and cloning them on demand
- Undo/redo systems that snapshot and restore object state via cloning
Code Examples
Cloneable Shape hierarchy using structuredClone for deep copying. Each shape implements a clone method that returns a fully independent copy, allowing modifications without affecting the original.
Related Patterns
Builder
Separate the construction of a complex object from its representation so the same construction process can create different representations.