原型模式(Prototype Pattern),prototypepattern

來源:互聯網
上載者:User

原型模式(Prototype Pattern),prototypepattern

                                           (五)原型模式(Prototype Pattern)

1.原型模式(Prototype Pattern)是用於建立重複的對象,同時又能保證效能。這種類型的設計模式屬於建立型模式,它提供了一種建立對象的最佳方式。

2.這種模式是實現了一個原型介面,該介面用於建立當前對象的複製。當直接建立對象的代價比較大時,則採用這種模式。例如,一個對象需要在一個高代價的資料庫操作之後被建立。我們可以緩衝該對象,在下一個請求時返回它的複製,在需要的時候更新資料庫,以此來減少資料庫調用。

3.

 

 

原型模式就是從一個對象再建立另外一個可制定的對象,而且不需要知道任何建立的細節。

4.

實現

我們將建立一個抽象類別 Shape 和擴充了 Shape 類的實體類。下一步是定義類 ShapeCache,該類把 shape Object Storage Service在一個 Hashtable 中,並在請求的時候返回它們的複製。

PrototypPatternDemo,我們的示範類使用 ShapeCache 類來擷取 Shape 對象。

步驟 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();   }   // 對每種形狀都運行資料庫查詢,並建立該形狀   // 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);   }}
步驟 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 : CircleShape : SquareShape : Rectangle

5.總結:一般在初始化的資訊不發生變化的情況下,複製是最好的辦法。這既隱藏了對象建立的細節,又對效能是大大的提高。(可參考:http://www.runoob.com/design-pattern/prototype-pattern.html)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.