December 16, 2011 Leave a comment
Cloning an object by reducing the cost of creation.
Where to use & benefits
- When there are many subclasses that differ only in the kind of objects,
- A system needs independent of how its objects are created, composed, and represented.
- Dynamic binding or loading a method.
- Use one instance to finish job just by changing its state or parameters.
- Add and remove objects at runtime.
- Specify new objects by changing its structure.
- Configure an application with classes dynamically.
The prototype pattern is a creational design pattern. In the prototype pattern, a new object is created by cloning an existing object. In JavaSW, the clone() method is an implementation of this design pattern. The prototype pattern can be a useful way of creating copies of objects. One example of how this can be useful is if an original object is created with a resource such as a data stream that may not be available at the time that a clone of the object is needed. Another example is if the original object creation involves a significant time commitment, such as reading data from a databaseW or over a network. An added benefit of the prototype pattern is that it can reduce class proliferation in a project by avoiding factory proliferation.