您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“java中的原型模式是什么”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“java中的原型模式是什么”吧!
引例
原型模式
淺拷貝
在原先Sheep類基礎(chǔ)上實(shí)現(xiàn)Cloneable接口,重寫(xiě)clone方法。
客戶端調(diào)用
Sheep類
新添的Cow類
客戶端調(diào)用克隆
深拷貝
1.Cow類也實(shí)現(xiàn)Cloneable接口
Sheep類的clone再添加調(diào)用cow的clone
客戶端調(diào)用
1.Cow類實(shí)現(xiàn)序列化接口,不必實(shí)現(xiàn)Cloneable接口了
2.在Sheep類實(shí)現(xiàn)序列化接口
3.客戶端調(diào)用
問(wèn)題:
現(xiàn)在有一只羊(包含屬性:名字Dolly、年齡2),需要克隆10只屬性完全相同的羊。
一般解法:
定義Sheep類表示羊,包括構(gòu)造器、getter()和toString()。
public class Sheep { private String name; private int age; public Sheep(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } @Override public String toString() { return "Sheep{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
在客戶端實(shí)例化多利,然后再根據(jù)多利的屬性去實(shí)例化10只羊。
public class Client { public static void main(String[] args) { Sheep sheepDolly=new Sheep("Dolly",2); Sheep sheep1 = new Sheep(sheepDolly.getName(), sheepDolly.getAge()); Sheep sheep2 = new Sheep(sheepDolly.getName(), sheepDolly.getAge()); Sheep sheep3 = new Sheep(sheepDolly.getName(), sheepDolly.getAge()); //.... System.out.println(sheep1+",hashCode:"+sheep1.hashCode()); System.out.println(sheep2+",hashCode:"+sheep2.hashCode()); System.out.println(sheep3+",hashCode:"+sheep3.hashCode()); //... } }
運(yùn)行結(jié)果
優(yōu)缺點(diǎn):
這種方法是我們首先很容易就能想到的,也是絕大多數(shù)人的第一做法。
但缺點(diǎn)也很明顯,每次創(chuàng)建新對(duì)象時(shí)需要獲取原始對(duì)象的屬性,對(duì)象復(fù)雜時(shí)效率很低;此外不能動(dòng)態(tài)獲得對(duì)象運(yùn)行時(shí)的狀態(tài),若類增減屬性需要改動(dòng)代碼。
下面我們看下原型模式的解法。
原型模式(Prototype Pattern)是一種創(chuàng)建型設(shè)計(jì)模式,允許一個(gè)對(duì)象再創(chuàng)建另外一個(gè)可定制的對(duì)象,無(wú)需知道如何創(chuàng)建的細(xì)節(jié)。即用原型實(shí)例指定創(chuàng)建對(duì)象的種類,并且通過(guò)拷貝這些原型,創(chuàng)建新的對(duì)象。
工作原理:將原型對(duì)象傳給那個(gè)要發(fā)動(dòng)創(chuàng)建的對(duì)象,這個(gè)要發(fā)動(dòng)創(chuàng)建的對(duì)象通過(guò)請(qǐng)求原型對(duì)象拷貝它們自己來(lái)實(shí)施創(chuàng)建。即用基類Object的clone()方法或序列化。
UML類圖:
Prototype:原型類,聲明一個(gè)克隆自己的接口
ConcretePrototype: 具體的原型類, 實(shí)現(xiàn)一個(gè)克隆自己的操作
Client: 客戶端讓一個(gè)原型對(duì)象克隆自己,從而創(chuàng)建一個(gè)新的對(duì)象
原型模式又可分為淺拷貝和深拷貝,區(qū)別在于對(duì)引用數(shù)據(jù)類型的成員變量的拷貝,小朋友你是否有很多問(wèn)號(hào)? 不急 ,看完這兩種方法實(shí)現(xiàn)你就懂了。
public class Sheep implements Cloneable{ private String name; private int age; @Override protected Object clone() {//克隆該實(shí)例,使用默認(rèn)的clone方法來(lái)完成 Sheep sheep = null; try { sheep = (Sheep)super.clone(); } catch (Exception e) { System.out.println(e.getMessage()); } return sheep; } public Sheep(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Sheep{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
public class Client { public static void main(String[] args) { Sheep sheepDolly=new Sheep("Dolly",2); Sheep sheep1 = (Sheep)sheepDolly.clone(); Sheep sheep2 = (Sheep)sheepDolly.clone(); Sheep sheep3 = (Sheep)sheepDolly.clone(); //.... System.out.println("sheep1:"+sheep1+",hashCode:" + sheep1.hashCode()); System.out.println("sheep2:"+sheep2+",hashCode:" + sheep2.hashCode()); System.out.println("sheep3:"+sheep3+",hashCode:" + sheep3.hashCode()); //... } }
運(yùn)行結(jié)果
至此,原型模式的淺拷貝也成功克隆了三個(gè)對(duì)象,但是看進(jìn)度條發(fā)現(xiàn)并不簡(jiǎn)單。
現(xiàn)在小羊有了一個(gè)朋友小牛,Sheep類添加了一個(gè)引用屬性Cow,我們同樣再克隆一遍。
public class Sheep implements Cloneable{ private String name; private int age; public Cow friend;//新朋友Cow對(duì)象,其余不變 @Override protected Object clone() { Sheep sheep = null; try { sheep = (Sheep)super.clone(); } catch (Exception e) { System.out.println(e.getMessage()); } return sheep; } public Sheep(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Sheep{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
public class Cow { private String name; private int age; public Cow(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Cow{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
public class Client { public static void main(String[] args) { Sheep sheepDolly=new Sheep("Dolly",2); sheepDolly.friend=new Cow("Tom",1); //并實(shí)例化朋友 Sheep sheep1 = (Sheep)sheepDolly.clone(); Sheep sheep2 = (Sheep)sheepDolly.clone(); Sheep sheep3 = (Sheep)sheepDolly.clone(); //.... System.out.println("sheep1:"+sheep1+",hashCode:" + sheep1.hashCode()); System.out.println("sheep1.friend:"+sheep1.friend+",hashCode:" + sheep1.friend.hashCode()+'\n'); System.out.println("sheep2:"+sheep2+",hashCode:" + sheep2.hashCode()); System.out.println("sheep2.friend:"+sheep2.friend+",hashCode:" + sheep2.friend.hashCode()+'\n'); System.out.println("sheep3:"+sheep3+",hashCode:" + sheep3.hashCode()); System.out.println("sheep3.friend:"+sheep3.friend+",hashCode:" + sheep3.friend.hashCode()+'\n'); //... } }
運(yùn)行結(jié)果
通過(guò)運(yùn)行結(jié)果發(fā)現(xiàn),淺拷貝通過(guò)Object的clone()成功克隆實(shí)例化了三個(gè)新對(duì)象,但是并沒(méi)有克隆實(shí)例化對(duì)象中的引用屬性,也就是沒(méi)有克隆friend對(duì)象(禁止套娃 ),三個(gè)新克隆對(duì)象的friend還是指向原克隆前的friend,即同一個(gè)對(duì)象。
這樣的話,他們四個(gè)的friend是引用同一個(gè),若一個(gè)對(duì)象修改了friend屬性,勢(shì)必會(huì)影響其他三個(gè)對(duì)象的該成員變量值。
小結(jié):
淺拷貝是使用默認(rèn)的 clone()方法來(lái)實(shí)現(xiàn)
基本數(shù)據(jù)類型的成員變量,淺拷貝會(huì)直接進(jìn)行值傳遞(復(fù)制屬性值給新對(duì)象)。
引用數(shù)據(jù)類型的成員變量,淺拷貝會(huì)進(jìn)行引用傳遞(復(fù)制引用值(內(nèi)存地址)給新對(duì)象)。
方法一:
機(jī)靈的人兒看出,再clone一遍cow不就好了,但是手動(dòng)遞歸下去不推薦。
public class Cow implements Cloneable{ private String name; private int age; public Cow(String name, int age) { this.name = name; this.age = age; } //無(wú)引用類型,直接clone即可 @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); //直接拋出了,沒(méi)用try-catch } @Override public String toString() { return "Cow{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
public class Sheep implements Cloneable{ private String name; private int age; public Cow friend;//新朋友Cow對(duì)象,其余不變 @Override protected Object clone() throws CloneNotSupportedException { Object deep = null; //完成對(duì)基本數(shù)據(jù)類型(屬性)和String的克隆 deep = super.clone(); //對(duì)引用類型的屬性,進(jìn)行再次clone Sheep sheep = (Sheep)deep; sheep.friend = (Cow)friend.clone(); return sheep; } public Sheep(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Sheep{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
public class Client { public static void main(String[] args) throws CloneNotSupportedException { Sheep sheepDolly=new Sheep("Dolly",2); sheepDolly.friend=new Cow("Tom",1); //并實(shí)例化朋友 Sheep sheep1 = (Sheep)sheepDolly.clone(); Sheep sheep2 = (Sheep)sheepDolly.clone(); Sheep sheep3 = (Sheep)sheepDolly.clone(); //.... System.out.println("sheep1:"+sheep1+",hashCode:" + sheep1.hashCode()); System.out.println("sheep1.friend:"+sheep1.friend+",hashCode:" + sheep1.friend.hashCode()+'\n'); System.out.println("sheep2:"+sheep2+",hashCode:" + sheep2.hashCode()); System.out.println("sheep2.friend:"+sheep2.friend+",hashCode:" + sheep2.friend.hashCode()+'\n'); System.out.println("sheep3:"+sheep3+",hashCode:" + sheep3.hashCode()); System.out.println("sheep3.friend:"+sheep3.friend+",hashCode:" + sheep3.friend.hashCode()+'\n'); //... } }
運(yùn)行結(jié)果
方法二:
通過(guò)對(duì)象序列化實(shí)現(xiàn)深拷貝(推薦)
public class Cow implements Serializable { private String name; private int age; public Cow(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Cow{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
public class Sheep implements Serializable { //實(shí)現(xiàn)序列化接口 private String name; private int age; public Cow friend; public Sheep(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Sheep{" + "name='" + name + '\'' + ", age=" + age + '}'; } public Object deepClone() { //深拷貝 //創(chuàng)建流對(duì)象 ByteArrayOutputStream bos = null; ObjectOutputStream oos = null; ByteArrayInputStream bis = null; ObjectInputStream ois = null; try { //序列化 bos = new ByteArrayOutputStream(); oos = new ObjectOutputStream(bos); oos.writeObject(this); //當(dāng)前這個(gè)對(duì)象以對(duì)象流的方式輸出 //反序列化 bis = new ByteArrayInputStream(bos.toByteArray()); ois = new ObjectInputStream(bis); Sheep sheep = (Sheep) ois.readObject(); return sheep; } catch (Exception e) { e.printStackTrace(); return null; } finally { //關(guān)閉流 try { bos.close(); oos.close(); bis.close(); ois.close(); } catch (Exception e2) { System.out.println(e2.getMessage()); } } } }
public class Client { public static void main(String[] args) throws CloneNotSupportedException { Sheep sheepDolly=new Sheep("Dolly",2); sheepDolly.friend=new Cow("Tom",1); //并實(shí)例化朋友 Sheep sheep1 = (Sheep)sheepDolly.deepClone(); Sheep sheep2 = (Sheep)sheepDolly.deepClone(); Sheep sheep3 = (Sheep)sheepDolly.deepClone(); //.... System.out.println("sheep1:"+sheep1+",hashCode:" + sheep1.hashCode()); System.out.println("sheep1.friend:"+sheep1.friend+",hashCode:" + sheep1.friend.hashCode()+'\n'); System.out.println("sheep2:"+sheep2+",hashCode:" + sheep2.hashCode()); System.out.println("sheep2.friend:"+sheep2.friend+",hashCode:" + sheep2.friend.hashCode()+'\n'); System.out.println("sheep3:"+sheep3+",hashCode:" + sheep3.hashCode()); System.out.println("sheep3.friend:"+sheep3.friend+",hashCode:" + sheep3.friend.hashCode()+'\n'); //... } }
運(yùn)行結(jié)果
原型模式總結(jié):
創(chuàng)建新的對(duì)象比較復(fù)雜時(shí),可以利用原型模式簡(jiǎn)化對(duì)象的創(chuàng)建過(guò)程,同時(shí)也能夠提高效率
可以不用重新初始化對(duì)象,動(dòng)態(tài)地獲得對(duì)象運(yùn)行時(shí)的狀態(tài)。
如果原始對(duì)象發(fā)生變化(增加或者減少屬性),其它克隆對(duì)象的也會(huì)發(fā)生相應(yīng)的變化,無(wú)需修改代碼
若成員變量無(wú)引用類型,淺拷貝clone即可;若引用類型的成員變量很少,可考慮遞歸實(shí)現(xiàn)clone,否則推薦序列化。
到此,相信大家對(duì)“java中的原型模式是什么”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。