1. 程式人生 > >設計模式:原型模式|菜鳥教程

設計模式:原型模式|菜鳥教程

定義

原型模式(Prototype Pattern)是用於建立重複的物件,同時又能保證效能。這種型別的設計模式屬於建立型模式,它提供了一種建立物件的最佳方式。
這種模式是實現了一個原型介面,該介面用於建立當前物件的克隆。當直接建立物件的代價比較大時,則採用這種模式。例如,一個物件需要在一個高代價的資料庫操作之後被建立。我們可以快取該物件,在下一個請求時返回它的克隆,在需要的時候更新資料庫,以此來減少資料庫呼叫。

介紹

意圖
用原型例項指定建立物件的種類,並且通過拷貝這些原型建立新的物件。

主要解決
在執行期建立和刪除原型。

何時使用
1. 當一個系統應該獨立於它的產品建立,構成和表示時。
2. 當要例項化的類是在執行時刻指定時,例如,通過動態裝載。
3. 為了避免建立一個與產品類層次平行的工廠類層次時。
4. 當一個類的例項只能有幾個不同狀態組合中的一種時。建立相應數目的原型並克隆它們可能比每次用合適的狀態手工例項化該類更方便一些。

如何解決
利用已有的一個原型物件,快速地生成和原型物件一樣的例項。

關鍵程式碼
1. 實現克隆操作,在 JAVA 繼承 Cloneable,重寫 clone(),在 .NET 中可以使用 Object 類的 MemberwiseClone() 方法來實現物件的淺拷貝或通過序列化的方式來實現深拷貝。
2. 原型模式同樣用於隔離類物件的使用者和具體型別(易變類)之間的耦合關係,它同樣要求這些”易變類”擁有穩定的介面。

應用例項
1. 細胞分裂。
2. JAVA 中的 Object clone() 方法。

優點
1. 效能提高。
2. 逃避建構函式的約束。

缺點
1. 配備克隆方法需要對類的功能進行通盤考慮,這對於全新的類不是很難,但對於已有的類不一定很容易,特別當一個類引用不支援序列化的間接物件,或者引用含有迴圈結構的時候。
2. 必須實現 Cloneable 介面。
3. 逃避建構函式的約束。

使用場景
1. 資源優化場景。
2. 類初始化需要消化非常多的資源,這個資源包括資料、硬體資源等。
3. 效能和安全要求的場景。
4. 通過 new 產生一個物件需要非常繁瑣的資料準備或訪問許可權,則可以使用原型模式。
5. 一個物件多個修改者的場景。
6. 一個物件需要提供給其他物件訪問,而且各個呼叫者可能都需要修改其值時,可以考慮使用原型模式拷貝多個物件供呼叫者使用。
7. 在實際專案中,原型模式很少單獨出現,一般是和工廠方法模式一起出現,通過 clone 的方法建立一個物件,然後由工廠方法提供給呼叫者。原型模式已經與 Java 融為渾然一體,大家可以隨手拿來使用。

注意事項
與通過對一個類進行例項化來構造新物件不同的是,原型模式是通過拷貝一個現有物件生成新物件的。淺拷貝實現 Cloneable,重寫,深拷貝是通過實現 Serializable 讀取二進位制流。

實現

我們將建立一個抽象類 Shape 和擴充套件了 Shape 類的實體類。下一步是定義類 ShapeCache,該類把 shape 物件儲存在一個 Hashtable 中,並在請求的時候返回它們的克隆。
PrototypPatternDemo,我們的演示類使用 ShapeCache 類來獲取 Shape 物件。
原型模式的 UML 圖|center

使用CPP實現

Shape.h定義所有的類:

#include <iostream>
#include <string>
#include <map>

using namespace std;

class Shape
{
public:
    string getType();
    string getId();

    void setId(string id);

    virtual Shape* clone() = 0;
    virtual void draw() = 0;

public:
    string type;    
private:
    string id;
};


class Rectangle: public Shape
{
public:
    Rectangle();
    void draw();
    Shape* clone();
};

class Square: public Shape
{
public:
    Square();
    void draw();
    Shape* clone();
};

class Circle: public Shape
{
public:
    Circle();
    void draw();
    Shape* clone();
};

class ShapeCache
{
private:
    map<string, Shape *> shapeMap;
public:
    Shape* getShape(string shapeId);
    void loadCache();
};

定義抽象類Shape.cpp

#include "Shape.h"

string Shape::getType(){
    return this->type;
}

string Shape::getId(){
    return this->id;
}

void Shape::setId(string id){
    this->id = id;
}

定義實際的類:Rectangle Square Circle
Rectangle.cpp

#include "Shape.h"


Rectangle::Rectangle()
{
    this->type = "Rectangle";
}

void Rectangle::draw(){
    cout << "Inside Rectangle:draw() method" << endl;
}

Shape* Rectangle::clone(){
    Shape *shapeClone = new Rectangle();
    shapeClone->setId(this->getId());
    shapeClone->type = this->getType();
    return shapeClone;
}   

Square.cpp

#include "Shape.h"

Square::Square()
{
    this->type = "Square";
}

void Square::draw(){
    cout << "Inside Square:draw() method" << endl;
}

Shape* Square::clone(){
    Shape *shapeClone = new Square();
    shapeClone->setId(this->getId());
    shapeClone->type = this->getType();
    return shapeClone;
}   

Circle.cpp

#include "Shape.h"


Circle::Circle()
{
    this->type = "Circle";
}

void Circle::draw(){
    cout << "Inside Circle:draw() method" << endl;
}

Shape* Circle::clone(){
    Shape *shapeClone = new Circle();
    shapeClone->setId(this->getId());
    shapeClone->type = this->getType();
    return shapeClone;
}   

ShapeCache.cpp

#include "Shape.h"


Shape* ShapeCache::getShape(string shapeId){
    Shape* shape = shapeMap[shapeId];
    return shape;
}

void ShapeCache::loadCache(){
    Circle *circle = new Circle();
    circle->setId("1");
    this->shapeMap.insert(pair<string, Shape*>(circle->getId(), circle));

    Square *square = new Square();
    square->setId("2");
    this->shapeMap.insert(pair<string, Shape*>(square->getId(), square));

    Rectangle *rectangle = new Rectangle();
    rectangle->setId("3");
    this->shapeMap.insert(pair<string, Shape*>(rectangle->getId(), rectangle));
}

PrototypePatternDemo.cpp

#include "Shape.h"

int main(int argc, char** argv)
{
    ShapeCache* cache = new ShapeCache();
    cache->loadCache();
    Shape *clonedShape0 = cache->getShape("1");
    Shape *clonedShape1 = cache->getShape("2");
    Shape *clonedShape2 = cache->getShape("3");

    cout << "Shape :" << clonedShape0->getType() << endl;
    cout << "Shape :" << clonedShape1->getType() << endl;
    cout << "Shape :" << clonedShape2->getType() << endl;   

    return 0;
}

使用Java來實現

步驟 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 : Circle
Shape : Square
Shape : Rectangle

參考資料