Wednesday, December 2, 2015

Prototype

Prototype pattern is a creational pattern used to create an object that is a clone of a cached instance of the object type :





It means that :
  • client will call clone() instead of new operator
  • the return object will be an exact copy of an existing object (members value...)

Short example :

We can image an base class VirtualDesktop and the following subclasses : VirtualDesktopDeveloper, VirtualDesktopManager. The code should define a cache of VirtualDesktop instances, properly configured.
The Client would then call VirtualDesktopDeveloper.clone() to get a pre-configured desktop for developers.

Check list 


  • Add a clone() method to an existing "product" base class (or interface)
  • Design a handler that will keep a cache of prototypical objects. It can be encapsulated in a Factory class or in the base class of the prototype.
  • Design a factory method that may take an argument to find the correct prototype, call clone() on it and return the desired prototype object.
  • Client replaces all of its new calls with the factory method call




0 comments:

Post a Comment