define
Specify what kind of objects to create with prototype instances, and create new objects by copying these prototypes.
nature
Clone an object
Cloning is the means to generate object instances.
His role
-
Prototype
Abstract class that defines methods used to copy existing instances to generate new ones.
-
ConcretePrototype
Implements a method to copy an existing instance and generate a new one.
-
Client (user)
Create a new instance by copying the instance.
The sample code
/** * public interface Product extends Cloneable{void use(String s); Product createClone(); } /** * public class MessageBox implements Product{private char decochar; public MessageBox(char s){ this.decochar = s; } @Override public void use(String s) { System.out.println("use. " + s); } @Override public Product createClone() { Product product = null; try { product = (Product)clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return product; Public implements Product{private char ulchar; public implements Product{private char ulchar; public UnderlinePen(char c){ this.ulchar = c; } @Override public void use(String s) { System.out.println("use. " + s); } @Override public Product createClone() { Product product = null; try { product = (Product)clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return product; }} /** * Client manages existing classes, Public class Manager {private HashMap<String,Product> showCase = new HashMap<>(); public void register(String name,Product product){ showCase.put(name,product); } public Product createProduct(String prototypeName){ return showCase.get(prototypeName).createClone(); Public class Main {public static void Main (String args[]){Manager Manager = new Manager(); MessageBox messageBox = new MessageBox('m'); UnderlinePen underlinePen = new UnderlinePen('u'); manager.register("m",messageBox); manager.register("u",underlinePen); Product messageBoxClone = manager.createProduct("m"); Product underlinePenClone = manager.createProduct("u"); messageBoxClone.use("this is the messageBoxClone"); underlinePenClone.use("this is the underlineClone"); }}Copy the code
The results
use this is the messageBoxClone
use this is the underlineClone
Copy the code
function
- Create a new instance based on the existing one. The prototype pattern requires an object to implement an interface that can clone itself, so that a new object can be created by cloning the instance object itself.
- Copy the values of the prototype example properties for the cloned new object instance.
advantages
- Encapsulation, which hides implementation details from the customer and simply calls the clone method to get the object.
disadvantages
- Each prototype subclass needs to implement the Clone operation, especially in the case of reference type objects, the Clone method is more troublesome, and must be able to recursively clone all related objects correctly.
When to use
- If an object wants to be independent of the object it is using, the prototype pattern can be used to make the system only program to the interface, and when the system needs a new object, it can be cloned.
- If the class to be instantiated is specified dynamically at run time, the stereotype pattern can be used and the desired instance can be obtained by cloning the stereotype.