設計模式 - 原型模式



原型模式是指在考慮效能的情況下建立重複物件的模式。這種設計模式屬於建立型模式,因為它提供了一種建立物件最佳方式。

這種模式涉及實現一個原型介面,該介面指示建立當前物件的克隆。當直接建立物件代價很高時,使用這種模式。例如,物件需要在代價高昂的資料庫操作後建立。我們可以快取物件,在下次請求時返回其克隆,並在需要時更新資料庫,從而減少資料庫呼叫次數。

實現

我們將建立一個抽象類Shape和擴充套件Shape類的具體類。下一步定義一個ShapeCache類,它在Hashtable中儲存形狀物件,並在請求時返回它們的克隆。

我們的演示類PrototypPatternDemo將使用ShapeCache類來獲取Shape物件。

Prototype Pattern UML Diagram

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

步驟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.");
   }
}

步驟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);
   }
}

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

步驟5

驗證輸出。

Shape : Circle
Shape : Square
Shape : Rectangle
廣告