Use the Prototype pattern when a system should be independent of how its products are created, composed, and represented; and
The big question that emerges from Prototype is "how deep"--that is, when we clone a given object, are we cloning just that object, or all of its associated/referenced objects, as well? Put into practical terms, if we clone the Person object, which holds a field for "spouse" and a field for a collection of "children" Person objects, do we clone the Person's spouse and any children objects as well? Are we cloning the entire family, or just the individual Person object? In some cases, if the referenced object is a Value Object,
A couple of interesting questions emerge when thinking about implementing a Prototype:
Where is the creator? The classic GOF pattern suggests that any instantiated object (that is, any object that is participating in the Prototype construction scheme) can be cloned, but this means that we get a cloned copy of that particular object, when in fact we may want a "pristine" clone to work from. Choosing the latter takes us closer to the Builder, since now that particular "source" object becomes the source of created objects, but it can be useful to have a small subset of Prototype-generated objects serving as "templates" for constructing other objects of similar form. In any event, it's important to understand whether cloned objects can in turn create clones, or whether that is intended to be scoped to a particular "source".
Last updated: 09 March 2022Tags: pattern creational