Design Patterns - prototype mode (ProtoType)

definition

用于创建重复的对象,同时又能保证性能。它属于创建型设计模式,它提供了一种创建对象的最佳方法。

template

Prototype (Prototype)
  Product role responsible for defining the method for duplicating an existing instance to generate a new instance. In the sample program, the Product interfaces play this role.
  
ConcretePrototype (specific prototype)
  ConcretePrototype role is responsible for implementation duplicating an existing instance and a new instance. In the sample program, the MessageBox class and UnderlinePen like to play this role.

Client (user)
  Client role responsible for generating new instances to use replicated instance. In the sample program, the Manager class to play this role.

Examples

  1. Creating a realization of the abstract class Shape Cloneable interface (Prototype):
public abstract class Shape implements Cloneable {
   
   private String id;
   protected String type;
   
   abstract void draw();
   
   public String getType(){
      return type;
   }
   
   public String getId() {
      return id;
   }
 
   public void setId(String id) {
      this.id = id;
   }
   
   public Object clone() {
      Object clone = null;
      try {
         clone = super.clone();
      } catch (CloneNotSupportedException e) {
         e.printStackTrace();
      }
      return clone;
   }
}
  1. Definition of a specific category (ConcretePrototype)
//Rectangle
public class Rectangle extends Shape {
 
   public Rectangle(){
     type = "Rectangle";
   }
 
   @Override
   public void draw() {
      System.out.println("Inside Rectangle::draw() method.");
   }
}

//Square
public class Square extends Shape {
 
   public Square(){
     type = "Square";
   }
 
   @Override
   public void draw() {
      System.out.println("Inside Square::draw() method.");
   }
}

//Circle
public class Circle extends Shape {
 
   public Circle(){
     type = "Circle";
   }
 
   @Override
   public void draw() {
      System.out.println("Inside Circle::draw() method.");
   }
}
  1. Management (equivalent to Client)
public class ShapeCache {
    
   private static Hashtable<String, Shape> shapeMap 
      = new Hashtable<String, Shape>();
 
   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);
   }
}
  1. test program
public class PrototypePatternDemo {
   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());        
   }
}

Output:

Shape : Circle
Shape : Square
Shape : Rectangle

advantage

Applicable scene is actually below its advantages

Shortcoming

  1. Create multiple classes of reading the code complexity (all design patterns common problem)
  2. just shallow copy clone method, in addition to the five base types types are simply a reference point, not re-create member variables, custom clone method For implementation needs.
  3. The constructor method is not called by the class clone, some scenes (do some initialization operations constructor) requires additional processing

Applicable scene

(1) a wide range of objects, when they are unable to integrate into a class
(2) The time class is difficult to generate an instance
too complicated process of generating the examples, it is difficult to generate the instance according to the class, in general, and before the user wants to generate a when exactly the same instance created out through the walkthrough, we will advance the user to save them by example operation creates it, and then generates a new instance by copying when needed.
(3) think when decoupling frame and generated instances
want the frame to generate an instance does not depend on the specific class, then you can not specify the class name to create an instance, prior to "register a prototype of" instance, and then copy this example generates a new instance.

Other examples:

To be added

Guess you like

Origin www.cnblogs.com/NeilZhang/p/11962151.html