Monday, 18 April 2016

Prototype pattern - Creational


  • Cloning an object by reducing the cost of creation
  • Create new objects by copying the prototype
  • Creates duplicate objects efficiently
  • It implements a prototype interface which tells to create a clone of the current object
  • Used
 
  



Where to use ?
  • When direct creation of object is costly
  • To use one instance to finish job just by changing its state or parameters.

Implementation example
  • Abstract class Color implementing Clonable interface and overriding clone method.
  • Multiple concrete classes Red, Green, Blue extending Color class.
  • A cache class maintaining a Hashtable of Color type objects and 2 methods :
    • Initialize cache : Make all type of objects and keep them in Hashtable
    • Get object : Get object from Hashtable by calling clone method
  • Client calls the cache class to initialize the cache (one time only on startup)
    Then, client calls cache class to get the object by passing a key of Hashtable

No comments:

Post a Comment

Note: only a member of this blog may post a comment.