Prototype pattern (Prototype Pattern) is used to create repeated objects, while ensuring performance. This type of design pattern is a creational pattern, which provides an optimal way to create objects.
This model implements a prototype interface that is used to create a clone of the current object. This model is used when the cost of directly creating an object is relatively high. For example, an object needs to be created after a costly database operation. We can cache the object and return its clone on the next request to reduce database calls.
There are three debut characters in the prototype mode:
Prototype role : define a method for copying an existing instance to generate a new instance;
Specific prototype role : Implement a method for copying an existing instance to generate a new instance
User role : Maintain a registry and provide a way to find the correct instance prototype. Finally, a method for obtaining a new instance is provided, which is used to delegate the method of copying an instance to generate a new instance.
Introduction
Intent: Use prototype instances to specify the types of objects created, and create new objects by copying these prototypes.
How to solve: Use an existing prototype object to quickly generate the same instance as the prototype object.
Key code: 1. Implement clone operation, inherit Cloneable in JAVA, and override clone (). In .NET, you can use MemberwiseClone () method of Object class to realize shallow copy of object or deep copy by serialization. 2. The prototype mode is also used to isolate the coupling relationship between the users of the class objects and the specific types (variable classes). It also requires these "variable classes" to have stable interfaces.
Advantages: 1. Improved performance. 2. Escape the constraints of the constructor.
Disadvantages: 1. The cloning method needs to consider the function of the class. This is not difficult for a new class, but it is not easy for an existing class, especially when a class references an indirect object that does not support serialization. Or when referring to a loop structure. 2. The Cloneable interface must be implemented
achieve
We will create an abstract class Shape and an entity class that extends the Shape class. The next step is to define the class ShapeCache , which stores shape objects in a Hashtable and returns their clones when requested.
PrototypePatternDemo , our demo class uses the ShapeCache class to get the Shape object.
The first step is to create a prototype role and implement the Cloneable interface
package prototype.demo;
/**
* 原形角色抽象类并实现了Cloneable接口
*/
public abstract class Shape implements Cloneable {
private String id;
protected String type;
public String getType(){
return type;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
/**
* 定义复制现有实例来生成新实例的方法
* @return
*/
public Object clone() {
Object clone = null;
try {
clone = super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return clone;
}
}
The second step, the specific prototype role
package prototype.demo;
/**
* 具体原型角色
*/
public class Circle extends Shape {
public Circle(){
type = "Circle";
}
}
package prototype.demo;
public class Rectangle extends Shape {
public Rectangle(){
type = "Rectangle";
}
}
package prototype.demo;
public class Square extends Shape {
public Square(){
type = "Square";
}
}
The third step is to create a user role: maintain a registry and provide a way to find the correct instance prototype. Finally, a method for obtaining a new instance is provided, which is used to delegate the method of copying an instance to generate a new instance.
package prototype.demo;
import java.util.Hashtable;
/**
* 使用者角色:维护一个注册表,并提供一个找出正确实例原型的方法。
* 最后,提供一个获取新实例的方法,用来委托复制实例的方法生成新实例
*/
public class ShapeCache {
private static Hashtable<String, Shape> shapeMap = new Hashtable();
/**
* 提供一个获取新实例的方法
* @param shapeId
* @return
*/
public static Shape getShape(String shapeId) {
Shape cachedShape = shapeMap.get(shapeId);//提供一个找出正确实例原型的方法
return (Shape) cachedShape.clone(); //委托复制实例的方法生成新实例。
}
// 对每种形状都运行数据库查询,并创建该形状
// shapeMap.put(shapeKey, shape);
// 例如,我们要添加三种形状
public static void loadCache() {
Circle circle = new Circle();
circle.setId("1");
shapeMap.put(circle.getId(),circle);
Square square = new Square();
square.setId("2");
shapeMap.put(square.getId(),square);
Rectangle rectangle = new Rectangle();
rectangle.setId("3");
shapeMap.put(rectangle.getId(),rectangle);
}
}
Start the test below
package prototype.demo;
public class Client {
public static void main(String[] args) {
ShapeCache.loadCache();
Shape clonedShape = (Shape) ShapeCache.getShape("1");
System.out.println("Shape : " + clonedShape.getType());
Shape clonedShape2 = (Shape) ShapeCache.getShape("2");
System.out.println("Shape : " + clonedShape2.getType());
Shape clonedShape3 = (Shape) ShapeCache.getShape("3");
System.out.println("Shape : " + clonedShape3.getType());
}
}
Shape : Circle
Shape : Square
Shape : Rectangle