Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. - Gof
The Prototype pattern creates a new object by cloning an existing object. The client using the prototype object does not need to know what kind of object it is dealing with as long as the concrete prototype extends or implements the prototype interface/class. The concrete prototype object is responsible for cloning itself and returning the cloned object.
The pattern enables a client to create the kind of object required at runtime by selecting the appropriate prototype. The prototype classes are created generically by the client without the client knowing the exact type of the concrete prototype. New concrete prototypes can be added at runtime as long as they conform to the abstract prototype.
Java Sample Code
Download Example 1
The following is an example of the Prototype Pattern. The prototype object is an Animal object. The Animal prototype contains two concrete prototype subclasses called Sheep and Chicken. The AnimalCreator class contains references to the two concrete prototypes. During the initialization of the AnimalCreator class the two concrete prototypes, Sheep and Chicken are created and stored as the two concrete prototypes members of the AnimalCreator class. The AnimalCreator class contains a retrieveAnimal method that clones a prototype Animal depending on the parameter that is passed to it.
The Animal class is the abstract prototype of the two concrete prototypes in the example. The client invokes methods on the two different concrete prototypes through the Animal type to ensure the client does not know the type of the concrete prototypes.
Most importantly, the Animal prototype defines a clone method to assist the two subtypes or concrete prototypes to clone themselves.
The Sheep object is a concrete prototype that extends the Animal prototype. The Sheep prototype has a clone method to clone itself to create a new object.
The Chicken object is a concrete prototype that extends the Animal prototype. The Chicken prototype has a clone method to clone itself to create a new object.
The AnimalCreator class is used to create and manage prototype objects. The AnimalCreator class contains two concrete prototypes that are initialized during the initialization of the class. The AnimalCreator class forms part of the "Prototype" pattern by returning a cloned object (Animal) to the client without the client knowing the type of the prototype.
The AnimalClient class makes use of the AnimalCreator class to create a concrete prototypes of type Animal. The AnimalClient class does not know the type of the concrete prototypes but references them through the Animal prototype.
The following is the output of the AnimalClient calling different concrete prototype objects.
Cluck cluck World. I am Chicken1. I have 2 legs.
Cluck cluck World. I am Chicken2. I have 2 legs.
Cluck cluck World. I am Chicken3. I have 2 legs.
Cluck cluck World. I am Chicken4. I have 2 legs.
Meeeeeee World. I am Sheep1. I have 4 legs.
Meeeeeee World. I am Sheep2. I have 4 legs.
Meeeeeee World. I am Sheep3. I have 4 legs.
Meeeeeee World. I am Sheep4. I have 4 legs.
Class Diagram Example
- Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley, 1995