Prototype design model (prototype)

  Prototype design model (prototype) 

  Panchiao里人http://www.jdon.com 2002/05/07 

  Definition: 

  Designated by creating prototype examples of the types of objects, and through the creation of new prototype copies of these items. 

  Prototype mode allows an object to create another object can be customized, did not need to know any details of how to create, working principle is: will be a prototype objects pass that it is necessary to mobilize to create objects, the objects created to mobilize through the request copies of the prototype object to the implementation of their own creation. 

  How to use it? 

  As provided in the Java clone () method to achieve Object clone (specific understanding clone () click here), so Prototype mode suddenly become very simple. 

  To spoon example: 

  Public abstract class AbstractSpoon implements Cloneable 
  ( 
  String spoonName; 

  Public void setSpoonName (String spoonName) = (this.spoonName spoonName;) 
  Public String getSpoonName () (return this.spoonName;) 

  Public Object clone () 
  ( 
  Object object = null; 
  Try ( 
  Object = super.clone (); 
  ) Catch (CloneNotSupportedException exception) ( 
  System.err.println ( "AbstractSpoon is not Cloneable"); 
  ) 
  Return object; 
  ) 
  ) 

  There are two concrete realization (ConcretePrototype): 

  Public class SoupSpoon extends AbstractSpoon 
  ( 
  Public SoupSpoon () 
  ( 
  SetSpoonName ( "Soup Spoon"); 
  ) 
  ) 

  Public class SaladSpoon extends AbstractSpoon 
  ( 
  Public SaladSpoon () 
  ( 
  SetSpoonName ( "Salad Spoon"); 
  ) 
  ) 

  Application Prototype model is very simple: 

  AbstractSpoon spoon = new SoupSpoon (); 
  AbstractSpoon spoon = new SaladSpoon (); 

  Of course, the factory model also can be combined to create AbstractSpoon examples. 

  Prototype Model in Java into a clone () method the use of Java as pure object-oriented features, makes use of Java design patterns become very nature, the two have had almost seamless.    This is reflected in many modes, such as Interator traversal mode. 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Tags:

Releated Articles


0 Comments to “Prototype design model (prototype)”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.