设计模式-原型模式(Prototype)
-
Specify the kinds of objects to create using a prototypical instance,and create new objects bycopying this prototype.(用原型實(shí)例指定創(chuàng)建對(duì)象的種類(lèi),并且通過(guò)拷貝這些原型創(chuàng)建新的對(duì)象。)簡(jiǎn)單說(shuō)明就是不通過(guò)new關(guān)鍵字產(chǎn)生對(duì)象,而是通過(guò)對(duì)象復(fù)制來(lái)實(shí)現(xiàn)就叫做原型模式。
原型模式的核心就是一個(gè)clone()方法,通過(guò)該方法進(jìn)行對(duì)象拷貝,java底層提供了一個(gè)Cloneable接口來(lái)標(biāo)示這個(gè)對(duì)象是可拷貝的,這個(gè)借口只是一個(gè)標(biāo)記作用,在JVM中有這個(gè)標(biāo)記的對(duì)象才能被拷貝。
原型模式注意事項(xiàng)
1.構(gòu)造函數(shù)不會(huì)被執(zhí)行。我們舉例說(shuō)明為什么不會(huì)執(zhí)行構(gòu)造函數(shù)。
/*** @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("我是一輛跑車(chē)...");}@Overridepublic Car clone() {Car car = null;try{car = (Car)super.clone();}catch (Exception e) {e.printStackTrace();}return car;} }客戶(hù)端
/*** @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();} }運(yùn)行結(jié)果
?
?
從運(yùn)行結(jié)果我們可以看出只打印一句話(huà) 說(shuō)明構(gòu)造函數(shù)沒(méi)執(zhí)行。Object類(lèi)的clone方法的原理是從內(nèi)存中(具體地說(shuō)就是堆內(nèi)存)以二進(jìn)制流的方式進(jìn)行拷貝,重新分配一個(gè)內(nèi)存塊,那構(gòu)造函數(shù)沒(méi)有被執(zhí)行也是非常正常的了。
淺拷貝和深拷貝
淺拷貝例子
/*** 淺拷貝* @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;}}客戶(hù)端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());*/} }執(zhí)行結(jié)果
?
?
?
執(zhí)行結(jié)果是兩個(gè),因?yàn)閖ava做了一個(gè)偷懶的動(dòng)作,clone方法只拷貝對(duì)象,對(duì)象內(nèi)的數(shù)組和引用對(duì)象等都不拷貝,這就是淺拷貝。兩個(gè)對(duì)象共享一個(gè)變量是一種非常不安全的方式。但是String字符串比較特殊,通過(guò)字符串池(stringpool)在需要的時(shí)候才在內(nèi)存中創(chuàng)建新的字符串。所以使用String時(shí)候可以當(dāng)做基本類(lèi)型就可以。
深拷貝例子
/*** @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;}}客戶(hù)端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());} }執(zhí)行結(jié)果
?
?
從上圖可以看出第一個(gè)執(zhí)行結(jié)果只有一個(gè)值。這就是深拷貝。
原型模式優(yōu)點(diǎn)
1.性能好,因?yàn)槭嵌M(jìn)制拷貝,比new一個(gè)對(duì)象性能好。
2.不依賴(lài)構(gòu)造函數(shù)
總結(jié)
以上是生活随笔為你收集整理的设计模式-原型模式(Prototype)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 设计模式 - 建造者模式(Builder
- 下一篇: ThreadLocal原理及用法详解