鍍金池/ 教程/ Java/ Java原型模式
Java前端控制器模式
Java工廠設(shè)計(jì)模式
Java抽象工廠模式
Java觀察者模式
Java門面模式(或外觀模式)
Java備忘錄模式
Java MVC模式
Java單例模式
Java傳輸對(duì)象模式
Java迭代器模式
Java責(zé)任鏈模式
Java命令模式
Java原型模式
Java解釋器模式
Java適配器模式
Java狀態(tài)模式
Java中介者模式(Mediator Pattern)
Java攔截過濾器模式
Java策略模式
Java組合模式
Java業(yè)務(wù)代理模式
Java裝飾模式
Java模板模式
Java橋接模式
Java過濾器模式(條件模式)
Java享元模式(Flyweight Pattern)
Java建造者(Builder)模式
Java設(shè)計(jì)模式
Java空對(duì)象模式
Java數(shù)據(jù)訪問對(duì)象模式
Java訪問者模式
Java組合實(shí)體模式
Java服務(wù)定位器模式

Java原型模式

原型模式指在創(chuàng)建重復(fù)對(duì)象的同時(shí)保持性能。 這種類型的設(shè)計(jì)模式屬于創(chuàng)建模式,因?yàn)榇四J教峁┝藙?chuàng)建對(duì)象的最佳方法之一。

這個(gè)模式涉及實(shí)現(xiàn)一個(gè)原型接口,它只創(chuàng)建當(dāng)前對(duì)象的克隆。有時(shí)直接創(chuàng)建對(duì)象時(shí)使用這種模式是昂貴的。例如,在昂貴的數(shù)據(jù)庫(kù)操作之后創(chuàng)建對(duì)象。因此我們可以緩存對(duì)象,在下一個(gè)請(qǐng)求時(shí)返回其克隆,并在需要時(shí)更新數(shù)據(jù)庫(kù),從而減少數(shù)據(jù)庫(kù)調(diào)用。

實(shí)現(xiàn)實(shí)例

我們將創(chuàng)建一個(gè)抽象類Shape和擴(kuò)展Shape類的具體類。 在下一步中定義ShapeCache類,在Hashtable中存儲(chǔ)形狀(Shape)對(duì)象,并在請(qǐng)求時(shí)返回其克隆。

PrototypPatternDemo這是一個(gè)演示類,將使用ShapeCache類來獲取一個(gè)Shape對(duì)象。實(shí)現(xiàn)結(jié)構(gòu)圖如下所示 -

第1步

創(chuàng)建一個(gè)實(shí)現(xiàn)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步

創(chuàng)建擴(kuò)展上述類的具體類。

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步

創(chuàng)建一個(gè)類來獲取具體的類,并將它們存儲(chǔ)在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類來獲取存儲(chǔ)在Hashtable中的形狀(shape)的克隆。

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步

驗(yàn)證輸出,執(zhí)行上面的代碼得到以下結(jié)果 -

Shape : Circle
Shape : Square
Shape : Rectangle