Prototype Pattern
Last updated
Last updated
Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype instead of depending on the new operator.
When the object instantiation from the beginning takes a lot of time.
When we want to specify an instance of the class to use it as a breeder of all future instances.
To avoid building a class hierarchy (seperate classes or factories) that parallels the class hierarchy of the products.
If we want to instantiate the same class with a minimal change of state, it's more appropriate to clone the instance and change its state to the required state.
Prototype: defines the interface for cloning
ConcretePrototype: implements the operation for cloning itself.
Client: creates a new object by asking a prototype to clone itself.
Registry: optional class that saves the frequently used prototypes, either before compilation, or even in runtime.
You can find the tests here.