设计模式-原型模式(Prototype)
生活随笔
收集整理的這篇文章主要介紹了
设计模式-原型模式(Prototype)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
-
Specify the kinds of objects to create using a prototypical instance,and create new objects bycopying this prototype.(用原型實例指定創建對象的種類,并且通過拷貝這些原型創建新的對象。)簡單說明就是不通過new關鍵字產生對象,而是通過對象復制來實現就叫做原型模式。
原型模式的核心就是一個clone()方法,通過該方法進行對象拷貝,java底層提供了一個Cloneable接口來標示這個對象是可拷貝的,這個借口只是一個標記作用,在JVM中有這個標記的對象才能被拷貝。
原型模式注意事項
1.構造函數不會被執行。我們舉例說明為什么不會執行構造函數。
/*** @author shuliangzhao* @Title: Car* @ProjectName design-parent* @Description: TODO* @date 2019/5/31 22:57*/ public class Car implements Cloneable{public Car() {System.out.println("我是一輛跑車...");}@Overridepublic Car clone() {Car car = null;try{car = (Car)super.clone();}catch (Exception e) {e.printStackTrace();}return car;} }客戶端
/*** @author shuliangzhao* @Title: CarClient* @ProjectName design-parent* @Description: TODO* @date 2019/5/31 22:59*/ public class CarClient {public static void main(String[] args) {Car car = new Car();Car clone = car.clone();} }運行結果
?
?
從運行結果我們可以看出只打印一句話 說明構造函數沒執行。Object類的clone方法的原理是從內存中(具體地說就是堆內存)以二進制流的方式進行拷貝,重新分配一個內存塊,那構造函數沒有被執行也是非常正常的了。
淺拷貝和深拷貝
淺拷貝例子
/*** 淺拷貝* @author shuliangzhao* @Title: Thing* @ProjectName design-parent* @Description: TODO* @date 2019/5/31 22:26*/ public class ShallowThing implements Cloneable {private List<String> list = new ArrayList<String>();@Overridepublic ShallowThing clone() {ShallowThing thing = null;try {thing = (ShallowThing) super.clone();}catch (Exception e) {e.printStackTrace();}return thing;}public void setList(String s) {this.list.add(s);}public List<String> getList() {return this.list;}}客戶端Client
/*** @author shuliangzhao* @Title: Client* @ProjectName design-parent* @Description: TODO* @date 2019/5/31 22:30*/ public class Client {public static void main(String[] args) {//淺拷貝ShallowThing thing = new ShallowThing();thing.setList("張三");ShallowThing clone = thing.clone();clone.setList("李四");System.out.println(thing.getList());//深拷貝/* DeepThing deepThing = new DeepThing();deepThing.setList("王五");DeepThing clone1 = deepThing.clone();clone1.setList("張柳");System.out.println(deepThing.getList());System.out.println(clone1.getList());*/} }執行結果
?
?
?
執行結果是兩個,因為java做了一個偷懶的動作,clone方法只拷貝對象,對象內的數組和引用對象等都不拷貝,這就是淺拷貝。兩個對象共享一個變量是一種非常不安全的方式。但是String字符串比較特殊,通過字符串池(stringpool)在需要的時候才在內存中創建新的字符串。所以使用String時候可以當做基本類型就可以。
深拷貝例子
/*** @author shuliangzhao* @Title: DeepThing* @ProjectName design-parent* @Description: TODO* @date 2019/5/31 22:32*/ public class DeepThing implements Cloneable{private ArrayList<String> list = new ArrayList<String>();@Overridepublic DeepThing clone() {DeepThing thing = null;try {thing = (DeepThing) super.clone();this.list = (ArrayList<String>)this.list.clone();}catch (Exception e) {e.printStackTrace();}return thing;}public void setList(String s) {this.list.add(s);}public List<String> getList() {return this.list;}}客戶端Client
/*** @author shuliangzhao* @Title: Client* @ProjectName design-parent* @Description: TODO* @date 2019/5/31 22:30*/ public class Client {public static void main(String[] args) {//淺拷貝/*ShallowThing thing = new ShallowThing();thing.setList("張三");ShallowThing clone = thing.clone();clone.setList("李四");System.out.println(thing.getList());*///深拷貝DeepThing deepThing = new DeepThing();deepThing.setList("王五");DeepThing clone1 = deepThing.clone();clone1.setList("張柳");System.out.println(deepThing.getList());System.out.println(clone1.getList());} }執行結果
?
?
從上圖可以看出第一個執行結果只有一個值。這就是深拷貝。
原型模式優點
1.性能好,因為是二進制拷貝,比new一個對象性能好。
2.不依賴構造函數
總結
以上是生活随笔為你收集整理的设计模式-原型模式(Prototype)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设计模式 - 建造者模式(Builder
- 下一篇: 设计模式-装饰模式(Decorator