Definition: Use prototype instance to specify the type of items, and create new objects by copying these prototypes.
Prototype mode allows an object to create another customizable object, do not need to know any details of how to create, the working principle is: By passing a prototype item to the object to be created, this item to be created by requesting prototype The object is copied to the creation of themselves.
How to use it? Because the Clone () method provided in Java implements the clone of the article (specifically CLONE () is here), the Prototype mode implements a bit simple.
Take a spoon as an 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);}}}}}}}}}}}}}}}}
There are two concreteprototypes:
public class SoupSpoon extends AbstractSpoon {public SoupSpoon () {setSpoonName ( "Soup Spoon");}} public class SaladSpoon extends AbstractSpoon {public SaladSpoon () {setSpoonName ( "Salad Spoon");}}
It is very simple to use Prototype mode:
AbstractSpoon spoon = new Soupspoon (); AbstractSpoon Spoon = new saladspoon ();
Of course, you can also create an ABSTRACTSPOON instance in conjunction with plant mode.
In Java, Prototype mode is used to use the use of a Clone () method, due to the pure object-oriented characteristics of Java, make it very nature in Java, which is almost a bitter. This is reflected in many modes, such as Interactor traversal mode.