一、原型模式的定義與特點
原型(Prototype)模式的定義如下:用一個已經創建的實例作為原型,通過復制該原型對象來創建一個和原型相同或相似的新對象。它屬於創建型
設計模式,用於創建重復的對象,同時又能保證性能(用這種方式創建對象非常高效)。
二、原型模式優點
- 性能優良:原型模式是在內存二進制流的拷貝,要比new一個對象性能好很多,特別是在一個循環體類產生大量對象的時候更加明顯。
- 逃避構造函數的約束:這是優缺點共存的一點,直接在內存中拷貝,構造函數是不會執行的。
三、原型模式的使用場景
- 資源初始化場景:類初始化需要消耗非常多的資源的時候。
- 性能和安全要求的場景:通過new產生一個對象需要非常繁瑣的數據准備和訪問權限的時候。
- 一個對象多個修改者的場景:一個對象需要提供給其他對象訪問,而各個調用者可能都需要修改其值時考慮使用。
實際項目中原型模式很少單獨出現,一般和工廠模式一起出現,通過clone方法創建一個對象,然后由工廠方法提供給調用者。
四、原型模式的結構與實現
由於 Java 提供了對象的 clone() 方法,所以用 Java 實現原型模式很簡單,只需要實現Cloneable接口並重寫clone()方法,簡單程度僅次於單例模式和迭代器模式。
原型模式包含以下主要角色。
- 抽象原型類:規定了具體原型對象必須實現的接口。
- 具體原型類:實現抽象原型類的 clone() 方法,它是可被復制的對象。
- 訪問類:使用具體原型類中的 clone() 方法來復制新的對象。
其結構圖如圖所示:
代碼實現如下:
//具體原型類 class Realizetype implements Cloneable { Realizetype() { System.out.println("具體原型創建成功!"); } public Object clone() throws CloneNotSupportedException { System.out.println("具體原型復制成功!"); return (Realizetype)super.clone(); } } //原型模式的測試類 public class PrototypeTest { public static void main(String[] args)throws CloneNotSupportedException { Realizetype obj1=new Realizetype(); Realizetype obj2=(Realizetype)obj1.clone(); System.out.println("obj1==obj2?"+(obj1==obj2)); } }
結果如下:
具體原型創建成功! //此處構造方法在clone的時候並不會執行,因為對象是從內存以二進制流的方式進行拷貝,當然不會執行 具體原型復制成功! obj1==obj2?false //不是同一個對象
五、補充說明
1、構造方法clone時不會執行
構造方法在clone的時候並不會執行,因為對象是從內存以二進制流的方式進行拷貝,當然不會執行,如上例子中所示。
2、深拷貝、淺拷貝
如下代碼,就是淺拷貝:
class Thing2 implements Cloneable { private ArrayList<String> list = new ArrayList<String>(); @Override public Thing2 clone() { Thing2 thing = null; try { thing = (Thing2) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); System.out.println("克隆失敗"); } return thing; } public void setValue(String value) { this.list.add(value); } public ArrayList getValue() { return this.list; } } public class SimpleClone { public static void main(String[] args) { Thing2 thing = new Thing2(); thing.setValue("張三"); Thing2 cloneThing= thing.clone(); cloneThing.setValue("李四"); System.out.println(thing.getValue()); } }
測試結果輸出為:
原始對象:[張三, 李四]
克隆對象:[張三, 李四]
原型模式克隆出來的對象應該是相互獨立的,那么為什么會出現原始對象和克隆對象之間共享訪問呢?
這是因為object類的clone方法只拷貝本對象,其對象內部的數組,引用對象等都不拷貝,還是指向原生對象的內部元素地址,這種拷貝就是淺拷貝。兩個對象共用一個私有變量。這是一種非常不安全的方式。
原始類型會被拷貝(int,double,long。。。),String類型也會被拷貝;數組、引用類型不會被拷貝。
使用原型類型時,引用的成員變量必須滿足兩個條件才不會被拷貝:
- 是類的成員變量,而不是方法內的變量
- 必須是一個可變的引用對象,而不是一個原始類型或者不可變對象(比如final)
如下為深拷貝:
class Thing1 implements Cloneable { private ArrayList<String> list = new ArrayList<String>(); @Override public Thing1 clone() { Thing1 thing = null; try { thing = (Thing1) super.clone(); thing.list = (ArrayList) this.list.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); System.out.println("克隆失敗"); } return thing; } public void setValue(String value) { this.list.add(value); } public ArrayList getValue() { return this.list; } } public class DeepClone { public static void main(String[] args) { Thing1 thing = new Thing1(); thing.setValue("張三"); Thing1 cloneThing= thing.clone(); cloneThing.setValue("李四"); System.out.println("原始對象:"+thing.getValue()); System.out.println("克隆對象:"+cloneThing.getValue()); } }
輸出結果為:
原始對象:[張三]
克隆對象:[張三, 李四]
注意:
- 深拷貝和淺拷貝要分開實現,不然會導致程序變得非常復雜
- 帶有final類型的變量是不可以進行拷貝的,這樣是無法實現深拷貝。
這是因為final關鍵字的特性
對於一個final變量,如果是基本數據類型的變量,則其數值一旦在初始化之后便不能更改;如果是引用類型的變量,則在對其初始化之后便不能再讓其指向另一個對象。因此:要使用clone()方法,類的成員變量上不要增加final關鍵字。
3、原型模式的擴展
原型模式可擴展為帶原型管理器的原型模式,它在原型模式的基礎上增加了一個原型管理器 PrototypeManager 類。該類用 HashMap 保存多個復制的原型,Client 類可以通過管理器的 get(String id) 方法從中獲取復制的原型。其結構圖如圖所示:
代碼如下:
import java.util.*; interface Shape extends Cloneable { public Object clone(); //拷貝 public void countArea(); //計算面積 } class Circle implements Shape { public Object clone() { Circle w=null; try { w=(Circle)super.clone(); } catch(CloneNotSupportedException e) { System.out.println("拷貝圓失敗!"); } return w; } public void countArea() { int r=0; System.out.print("這是一個圓,請輸入圓的半徑:"); Scanner input=new Scanner(System.in); r=input.nextInt(); System.out.println("該圓的面積="+3.1415*r*r+"\n"); } } class Square implements Shape { public Object clone() { Square b=null; try { b=(Square)super.clone(); } catch(CloneNotSupportedException e) { System.out.println("拷貝正方形失敗!"); } return b; } public void countArea() { int a=0; System.out.print("這是一個正方形,請輸入它的邊長:"); Scanner input=new Scanner(System.in); a=input.nextInt(); System.out.println("該正方形的面積="+a*a+"\n"); } } class ProtoTypeManager { private HashMap<String, Shape>ht=new HashMap<String,Shape>(); public ProtoTypeManager() { ht.put("Circle",new Circle()); ht.put("Square",new Square()); } public void addshape(String key,Shape obj) { ht.put(key,obj); } public Shape getShape(String key) { Shape temp=ht.get(key); return (Shape) temp.clone(); } } public class ProtoTypeShape { public static void main(String[] args) { ProtoTypeManager pm=new ProtoTypeManager(); Shape obj1=(Circle)pm.getShape("Circle"); obj1.countArea(); Shape obj2=(Shape)pm.getShape("Square"); obj2.countArea(); } }
測試結果為:
這是一個圓,請輸入圓的半徑:3 該圓的面積=28.2735 這是一個正方形,請輸入它的邊長:3 該正方形的面積=9