Prototype mode Definition: Specify the type of object to create an object with a prototype instance and create a new object by copying these prototypes.
Prototype mode allows an object to create another customable object, do not need to know any details of how to create, the working principle is: By passing a prototype object to the object to be created, this object to be created by requesting prototype Objects copy them to implement creation.
How to use it? Because the Clone () method in Java provides the object's clone, the prototype mode achieves 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 is a specific implementation (ConcretePrototype):
Public Class Soupspoon Extends AbstractSpoon {PUBLIC SOUPSPOON () {setspoonname ("Soup Spoon");}}
Call the prototype mode is very simple:
AbstractSpoon spoon = new soupspoon (); abstractspoon spoon2 = spoon.clone ();
Of course, you can also create an ABSTRACTSPOON instance in conjunction with plant mode.
The prototype mode in Java becomes the use of the 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.