原型模式(Prototype Pattern)是用于創建重復的對象,同時又能保證性能。這種類型的設計模式屬于創建型模式,它提供了一種創建對象的最佳方式。
這種模式是實現了一個原型接口,該接口用于創建當前對象的克隆。當直接創建對象的代價比較大時,則采用這種模式。例如,一個對象需要在一個高代價的數據庫操作之后被創建。我們可以緩存該對象,在下一個請求時返回它的克隆,在需要的時候更新數據庫,以此來減少數據庫調用。
意圖:用原型實例指定創建對象的種類,并且通過拷貝這些原型創建新的對象。
主要解決:在運行期建立和刪除原型。
何時使用:
⒈當一個系統應該獨立于它的產品創建,構成和表示時。
⒉當要實例化的類是在運行時刻指定時,例如,通過動態裝載。
⒊為了避免創建一個與產品類層次平行的工廠類層次時。
⒋當一個類的實例只能有幾個不同狀態組合中的一種時。建立相應數目的原型并克隆它們可能比每次用合適的狀態手工實例化該類更方便一些。
如何解決:利用已有的一個原型對象,快速地生成和原型對象一樣的實例。
關鍵代碼:
⒈實現克隆操作,在 JAVA 繼承 Cloneable,重寫 clone(),在 .NET 中可以使用 Object 類的 MemberwiseClone() 方法來實現對象的淺拷貝或通過序列化的方式來實現深拷貝。
⒉原型模式同樣用于隔離類對象的使用者和具體類型(易變類)之間的耦合關系,它同樣要求這些"易變類"擁有穩定的接口。
應用實例:
⒈細胞分裂。
⒉JAVA 中的 Object clone() 方法。
優點:
⒈性能提高。
⒉逃避構造函數的約束。
缺點:
⒈配備克隆方法需要對類的功能進行通盤考慮,這對于全新的類不是很難,但對于已有的類不一定很容易,特別當一個類引用不支持串行化的間接對象,或者引用含有循環結構的時候。
⒉必須實現 Cloneable 接口。
使用場景:
⒈資源優化場景。
⒉類初始化需要消化非常多的資源,這個資源包括數據、硬件資源等。
⒊性能和安全要求的場景。
⒋通過 new 產生一個對象需要非常繁瑣的數據準備或訪問權限,則可以使用原型模式。
⒌一個對象多個修改者的場景。
⒍一個對象需要提供給其他對象訪問,而且各個調用者可能都需要修改其值時,可以考慮使用原型模式拷貝多個對象供調用者使用。
⒎在實際項目中,原型模式很少單獨出現,一般是和工廠方法模式一起出現,通過 clone 的方法創建一個對象,然后由工廠方法提供給調用者。原型模式已經與 Java 融為渾然一體,大家可以隨手拿來使用。
注意事項:與通過對一個類進行實例化來構造新對象不同的是,原型模式是通過拷貝一個現有對象生成新對象的。淺拷貝實現 Cloneable,重寫,深拷貝是通過實現 Serializable 讀取二進制流。
我們將創建一個抽象類 Shape 和擴展了 Shape 類的實體類。下一步是定義類 ShapeCache,該類把 shape 對象存儲在一個 Hashtable 中,并在請求的時候返回它們的克隆。
PrototypePatternDemo,我們的演示類使用 ShapeCache 類來獲取 Shape 對象。
步驟 1
創建一個實現了 Cloneable 接口的抽象類。
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
創建擴展了上面抽象類的實體類。
public class Rectangle extends Shape {
public Rectangle(){
type = "Rectangle";
}
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
public class Square extends Shape {
public Square(){
type = "Square";
}
@Override
public void draw() {
System.out.println("Inside Square::draw() method.");
}
}
public class Circle extends Shape {
public Circle(){
type = "Circle";
}
@Override
public void draw() {
System.out.println("Inside Circle::draw() method.");
}
}
步驟 3
創建一個類,從數據庫獲取實體類,并把它們存儲在一個 Hashtable 中。
import java.util.Hashtable;
public class ShapeCache {
private static Hashtable shapeMap
= new Hashtable();
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 中的形狀的克隆。
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