原型模式(Prototype Pattern)是用于創(chuàng)建重復(fù)的對(duì)象,同時(shí)又能保證性能。這種類型的設(shè)計(jì)模式屬于創(chuàng)建型模式,它提供了一種創(chuàng)建對(duì)象的最佳方式。
這種模式是實(shí)現(xiàn)了一個(gè)原型接口,該接口用于創(chuàng)建當(dāng)前對(duì)象的克隆。當(dāng)直接創(chuàng)建對(duì)象的代價(jià)比較大時(shí),則采用這種模式。例如,一個(gè)對(duì)象需要在一個(gè)高代價(jià)的數(shù)據(jù)庫操作之后被創(chuàng)建。我們可以緩存該對(duì)象,在下一個(gè)請(qǐng)求時(shí)返回它的克隆,在需要的時(shí)候更新數(shù)據(jù)庫,以此來減少數(shù)據(jù)庫調(diào)用。
介紹
意圖:
用原型實(shí)例指定創(chuàng)建對(duì)象的種類,并且通過拷貝這些原型創(chuàng)建新的對(duì)象。
主要解決:
在運(yùn)行期建立和刪除原型。
何時(shí)使用:
1、當(dāng)一個(gè)系統(tǒng)應(yīng)該獨(dú)立于它的產(chǎn)品創(chuàng)建,構(gòu)成和表示時(shí)。
2、當(dāng)要實(shí)例化的類是在運(yùn)行時(shí)刻指定時(shí),例如,通過動(dòng)態(tài)裝載。
3、為了避免創(chuàng)建一個(gè)與產(chǎn)品類層次平行的工廠類層次時(shí)。
4、當(dāng)一個(gè)類的實(shí)例只能有幾個(gè)不同狀態(tài)組合中的一種時(shí)。建立相應(yīng)數(shù)目的原型并克隆它們可能比每次用合適的狀態(tài)手工實(shí)例化該類更方便一些。
如何解決:
利用已有的一個(gè)原型對(duì)象,快速地生成和原型對(duì)象一樣的實(shí)例。
關(guān)鍵代碼:
1、實(shí)現(xiàn)克隆操作,在 JAVA 繼承 Cloneable,重寫 clone(),在 .NET 中可以使用 Object 類的 MemberwiseClone() 方法來實(shí)現(xiàn)對(duì)象的淺拷貝或通過序列化的方式來實(shí)現(xiàn)深拷貝。
2、原型模式同樣用于隔離類對(duì)象的使用者和具體類型(易變類)之間的耦合關(guān)系,它同樣要求這些"易變類"擁有穩(wěn)定的接口。
應(yīng)用實(shí)例:
1、細(xì)胞分裂。
2、JAVA 中的 Object clone() 方法。
優(yōu)點(diǎn):
1、性能提高。
2、逃避構(gòu)造函數(shù)的約束。
缺點(diǎn):
1、配備克隆方法需要對(duì)類的功能進(jìn)行通盤考慮,這對(duì)于全新的類不是很難,但對(duì)于已有的類不一定很容易,特別當(dāng)一個(gè)類引用不支持串行化的間接對(duì)象,或者引用含有循環(huán)結(jié)構(gòu)的時(shí)候。
2、必須實(shí)現(xiàn) Cloneable 接口。
使用場景:
1、資源優(yōu)化場景。
2、類初始化需要消化非常多的資源,這個(gè)資源包括數(shù)據(jù)、硬件資源等。
3、性能和安全要求的場景。
4、通過 new 產(chǎn)生一個(gè)對(duì)象需要非常繁瑣的數(shù)據(jù)準(zhǔn)備或訪問權(quán)限,則可以使用原型模式。
5、一個(gè)對(duì)象多個(gè)修改者的場景。
6、一個(gè)對(duì)象需要提供給其他對(duì)象訪問,而且各個(gè)調(diào)用者可能都需要修改其值時(shí),可以考慮使用原型模式拷貝多個(gè)對(duì)象供調(diào)用者使用。
7、在實(shí)際項(xiàng)目中,原型模式很少單獨(dú)出現(xiàn),一般是和工廠方法模式一起出現(xiàn),通過 clone 的方法創(chuàng)建一個(gè)對(duì)象,然后由工廠方法提供給調(diào)用者。原型模式已經(jīng)與 Java 融為渾然一體,大家可以隨手拿來使用。
注意事項(xiàng):
與通過對(duì)一個(gè)類進(jìn)行實(shí)例化來構(gòu)造新對(duì)象不同的是,原型模式是通過拷貝一個(gè)現(xiàn)有對(duì)象生成新對(duì)象的。淺拷貝實(shí)現(xiàn) Cloneable,重寫,深拷貝是通過實(shí)現(xiàn) Serializable 讀取二進(jìn)制流。
實(shí)現(xiàn)
我們將創(chuàng)建一個(gè)抽象類 Shape 和擴(kuò)展了 Shape 類的實(shí)體類。下一步是定義類 ShapeCache,該類把 shape 對(duì)象存儲(chǔ)在一個(gè) Hashtable 中,并在請(qǐng)求的時(shí)候返回它們的克隆。
PrototypePatternDemo,我們的演示類使用 ShapeCache 類來獲取 Shape 對(duì)象。
步驟 1
創(chuàng)建一個(gè)實(shí)現(xiàn)了 Cloneable 接口的抽象類。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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ò)展了上面抽象類的實(shí)體類。
1
2
3
4
5
6
7
8
9
10
11
|
public class Rectangle extends Shape { public Rectangle(){ type = "Rectangle" ; } @Override public void draw() { System.out.println( "Inside Rectangle::draw() method." ); } } |
1
2
3
4
5
6
7
8
9
10
11
|
public class Square extends Shape { public Square(){ type = "Square" ; } @Override public void draw() { System.out.println( "Inside Square::draw() method." ); } } |
1
2
3
4
5
6
7
8
9
10
11
|
public class Circle extends Shape { public Circle(){ type = "Circle" ; } @Override public void draw() { System.out.println( "Inside Circle::draw() method." ); } } |
步驟 3
創(chuàng)建一個(gè)類,從數(shù)據(jù)庫獲取實(shí)體類,并把它們存儲(chǔ)在一個(gè) Hashtable 中。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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(); } // 對(duì)每種形狀都運(yùn)行數(shù)據(jù)庫查詢,并創(chuàng)建該形狀 // 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 類來獲取存儲(chǔ)在 Hashtable 中的形狀的克隆。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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
執(zhí)行程序,輸出結(jié)果:
Shape : Circle
Shape : Square
Shape : Rectangle
以上就是詳解JAVA 原型模式的詳細(xì)內(nèi)容,更多關(guān)于JAVA 原型模式的資料請(qǐng)關(guān)注服務(wù)器之家其它相關(guān)文章!
原文鏈接:https://www.runoob.com/design-pattern/prototype-pattern.html