Rajagopal
Posts: 3
Nickname: rajagopaly
Registered: Mar, 2005
|
|
Re: What is prototype design pattern?
|
Posted: Mar 15, 2005 9:38 PM
|
|
Prototype pattern implies cloning of an object rather than creation. If the cost of creating a new object is large and creation is resource intensive, we clone the object. We use Cloneable interface and call it¡¦s clone() method on the object we want to clone.
Starts with an initialized and instantiated class and clones it to make new instances rather than creating new instances.
The Prototype pattern allows an object to create customized objects without knowing their exact class or the details of how to create them. It specifies the kinds of objects to create using a prototypical instance, and creates new objects by copying this prototype. The Prototype pattern works by giving prototypical objects to an object and then initiates the creation of objects. The creation-initiating object then creates objects by asking the prototypical objects to make copies of themselves. The Prototype pattern makes creating objects dynamically easier by defining classes whose objects can duplicate themselves.
The benefits of using the Prototype pattern: Ï Adding and removing products at run time Ï Specifying new objects by varying values Ï Specifying new objects by varying structure Ï Reduced subclassing Ï Configuring an application with classes dynamically
We should use the Prototype pattern when: Ï The classes to instantiate are specified at run time, for example, by dynamic loading. Ï To avoid building a class hierarchy of factories that parallels the class hierarchy of products. Ï When instances of a class can have one of only a few different combinations of state.
|
|