- Summary
- Prototype pattern uses cloning as object instantiation pattern. We can use prototype pattern in following scenario.
- When need to create a large number of objects that just differ in terms of their attribute values. For example rooms in a building construction project.
- In this case each object is derived from a common interface and have cloning logic (ex. Copy and paste operation )
- When all the object need to share some common schema loaded at runtime for example XML Document Objects.
- Overview Tutorial
- http://www.oodesign.com/prototype-pattern.html
- Class Diagram
- Client holds an instants of object that expose clone() method . Whenever client needs an object it uses clone() method to create a new object. Client can further update its properties if required.
- Specific problems and Considerations
- Using Prototype Manager for cloning
- We can use prototype manager that is implemented usually as
a hash table keeping the object to clone. In this cases prototype become a
factory method which uses cloning instead of Instantiation.
- Deep Clones vs. Shallow Clones
- When we clone complex objects which contains other objects,
we should take care how they are cloned.
- We can clone contained objects also
(deep cloning) or we can the same reference for them, and to share them between
cloned container objects.
- Explicit Object Initialization
- There are certain situations when objects need to be
initialized after they are created.
- Prototype Vs Factory Vs Builder
- In nut shell prototype uses replication, factory
uses instantiation and builder uses composition mechanism to create final
object.
- Use Prototype if final object differ only
attribute values not in schemas of object.
- Prototype with Prototype Manger become Factory
pattern that uses cloning as object initialization.
- Prototype is very useful when a family of object
need to share a common schema, reference
or constraints , best example is XMLDocument class.
|
|