目录

设计模式 - 原型模式( Prototype Pattern)

原型模式是指在保持性能的同时创建重复对象。 这种类型的设计模式属于创建模式,因为此模式提供了创建对象的最佳方法之一。

此模式涉及实现原型接口,该接口告诉创建当前对象的克隆。 当直接创建对象成本高时使用此模式。 例如,在昂贵的数据库操作之后创建对象。 我们可以缓存对象,在下一个请求时返回其克隆,并在需要时更新数据库,从而减少数据库调用。

实现 (Implementation)

我们将创建一个抽象类Shape和扩展Shape类的具体类。 类ShapeCache被定义为下一步,它将形状对象存储在Hashtable并在请求时返回它们的克隆。

PrototypPatternDemo ,我们的演示类将使用ShapeCache类来获取Shape对象。

原型模式UML图

Step 1

创建一个实现Clonable接口的抽象类。

Shape.java

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;
   }
}

Step 2

创建扩展上述类的具体类。

Rectangle.java

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

Square.java

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

Circle.java

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

Step 3

创建一个类以从数据库中获取具体类并将它们存储在Hashtable

ShapeCache.java

import java.util.Hashtable;
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();
   }
   // for each shape run database query and create shape
   // shapeMap.put(shapeKey, shape);
   // for example, we are adding three shapes
   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);
   }
}

Step 4

PrototypePatternDemo使用ShapeCache类来获取存储在Hashtable的形状克隆。

PrototypePatternDemo.java

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());		
   }
}

Step 5

验证输出。

Shape : Circle
Shape : Square
Shape : Rectangle
↑回到顶部↑
WIKI教程 @2018