Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
You need to create new objects that are similar to existing objects, and object creation is expensive or complex.
The Prototype pattern creates new objects by cloning existing objects (prototypes) rather than creating them from scratch.
- When the classes to instantiate are specified at runtime
- To avoid building a class hierarchy of factories parallel to product hierarchy
- When instances of a class can have only a few different combinations of state
- When object creation is expensive
See the Program.cs file for a shape registry demonstrating object cloning with prototypes.
- Can clone objects without coupling to their concrete classes
- Can get rid of repeated initialization code
- Convenient for producing complex objects
- Cloning complex objects with circular references might be tricky
- Deep copy vs shallow copy considerations