zhjr08-GoF創(chuàng)建型模式-原型模式.ppt_第1頁
zhjr08-GoF創(chuàng)建型模式-原型模式.ppt_第2頁
zhjr08-GoF創(chuàng)建型模式-原型模式.ppt_第3頁
zhjr08-GoF創(chuàng)建型模式-原型模式.ppt_第4頁
zhjr08-GoF創(chuàng)建型模式-原型模式.ppt_第5頁
已閱讀5頁,還剩29頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、設(shè)計模式,授課:張金榮 Email: QQ: (即1211460305) 電話GoF-創(chuàng)建型模式,第 3 章,3.1 Simple Factory Pattern(簡單工廠模式) 3.2 Factory Method Pattern(工廠方法模式) 3.3 Abstract Factory Pattern(抽象工廠模式) 3.4 Builder Pattern(建造者模式) 3.5 Prototype Pattern(原型模式) 3.6 Singleton Pattern(單例模式),GoF-創(chuàng)建型模式,第 3 章,Factory Pattern(工廠模式),Sing

2、leton Pattern(單例模式),本章重點:,本章難點:,Builder Pattern(建造者模式),Abstract Factory Pattern(抽象工廠模式),3.5 Prototype Pattern (原型模式),原型模式定義:用原型實例指定創(chuàng)建對象的種類,并且通過拷貝這些原型創(chuàng)建新的對象. Prototype模式允許一個對象再創(chuàng)建另外一個可定制的對象,根本無需知道任何如何創(chuàng)建的細節(jié),工作原理是:通過將一個原型對象傳給那個要發(fā)動創(chuàng)建的對象,這個要發(fā)動創(chuàng)建的對象通過請求原型對象拷貝它們自己來實施創(chuàng)建。,3.5 Prototype Pattern (原型模式),原型模式的類圖,

3、3.5 Prototype Pattern (原型模式),3.5 Prototype Pattern (原型模式),在java的類庫中已經(jīng)實現(xiàn)了這一模式,只要你定義的類實現(xiàn)了Cloneable接口,用這個類所創(chuàng)建的對象可以做為原型對象進而克隆出更多的同類型的對象。,在Java里提到clone技術(shù),就不能不提java.lang.Cloneable接口和含有clone方法的Object類。所有具有clone功能的類都有一個特性,那就是它直接或間接地實現(xiàn)了Cloneable接口。否則,我們在嘗試調(diào)用clone()方法時,將會觸發(fā)CloneNotSupportedException異常。,3.5 Pr

4、ototype Pattern (原型模式),3.5 Prototype Pattern (原型模式),publicinterfaceProtoTypeextendsCloneable publicObjectclone(); ,下面是實例,public class ConcreteProtoType implements ProtoType private String propA; private String propB; public Object clone() try return super.clone(); catch (CloneNotSupportedException e

5、) return null; ,3.5 Prototype Pattern (原型模式),public String getPropA() return propA; public void setPropA(String propA) pA = propA; public String getPropB() return propB; public void setPropB(String propB) pB = propB; ,3.5 Prototype Pattern (原型模式),public Object clone() object o=null;

6、try o=super.clone();return o; catch (CloneNotSupportedException e) return null; ,public class ConcreteProtoType implements ProtoType private String propA; private String propB; public ProtoType clone() try return (ProtoType)super.clone(); catch (CloneNotSupportedException e) return null; ,3.5 Protot

7、ype Pattern (原型模式),/這里重載了Obect#clone()方法,為了方便外部調(diào)用,把返回值由Object修改為ProtoType ,并強制類型轉(zhuǎn)換。* protected - public,3.5 Prototype Pattern (原型模式),public class Client public static void testMethod() ConcreteProtoType proto = new ConcreteProtoType(); proto.setPropA(propA); proto.setPropB(propB); ConcreteProtoType

8、newProto = (ConcreteProtoType) proto.clone(); 。打印newProto和Proto否的值看相等 ,以上樣例使用淺克隆方式實現(xiàn)proto type模式,淺克隆無法對對象中包含的其他非java原是類型數(shù)據(jù)進行復制。如果需要復制其他非java原是類型數(shù)據(jù),則需要使用深克隆方式,java實現(xiàn)shadow clone(淺克隆)與深克隆(deep clone),克隆就是復制一個對象的復本.但一個對象中可能有基本數(shù)據(jù)類型,如:int,long,float 等,也同時含有非基本數(shù)據(jù)類型如(數(shù)組,集合等)被克隆得到的對象基本類型的值修改了,原對象的值不會改變.這是sh

9、adow clone(淺克隆). 但如果你要改變一個非基本類型的值時,原對象的值卻改變了.比如一個數(shù)組,內(nèi)存中只copy他的地址,而這個地址指向的值并沒有copy,當clone時,兩個地址指向了一個值,這樣一旦這個值改變了,原來的值當然也變了,因為他們共用一個值.,這就必須得用深克隆(deep clone),java實現(xiàn)shadow clone(淺克隆)與深克隆(deep clone),淺克隆只適合克隆基本類型,對于引用類型就不能實現(xiàn)克隆了 在Java里深克隆一個對象,常??梢韵仁箤ο髮崿F(xiàn)Serializable接口,然后把對象(實際上只是對象的一個拷貝)寫到一個流里,再從流里讀回來,便可以重

10、建對象。,java實現(xiàn)shadow clone(淺克隆)與深克隆(deep clone),java.io.ObjectOutputStream代表對象輸出流,它的writeObject(Object obj)方法可對參數(shù)指定的obj對象進行序列化,把得到的字節(jié)序列寫到一個目標輸出流中。 java.io.ObjectInputStream代表對象輸入流,它的readObject()方法從一個源輸入流中讀取字節(jié)序列,再把它們反序列化為一個對象,并將其返回。,Java的序列化與反序列化,只有實現(xiàn)了Serializable和Externalizable接口的類的對象才能被序列化。Externaliza

11、ble接口繼承自Serializable接口,實現(xiàn)Externalizable接口的類完全由自身來控制序列化的行為,而僅實現(xiàn)Serializable接口的類可以采用默認的序列化方式 。,Java的序列化與反序列化,對象序列化包括如下步驟:1) 創(chuàng)建一個對象輸出流,它可以包裝一個其他類型的目標輸出流,如文件輸出流;2) 通過對象輸出流的writeObject()方法寫對象。 對象反序列化的步驟如下:1) 創(chuàng)建一個對象輸入流,它可以包裝一個其他類型的源輸入流,如文件輸入流;2) 通過對象輸入流的readObject()方法讀取對象。,Java的序列化與反序列化,class Customer imp

12、lements Serializable private String name;private int age;public Customer(String name, int age) = name;this.age = age;public String toString() return name= + name + , age= + age;,Java的序列化與反序列化,import java.io.*;import java.util.Date; public class ObjectSaver public static void main(String ar

13、gs) throws Exception ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(D:objectFile.obj); ,Java的序列化與反序列化,/序列化對象Customer customer = new Customer(阿蜜果, 24);out.writeObject(你好!);out.writeObject(new Date();out.writeObject(customer);out.writeInt(123); /寫入基本類型數(shù)據(jù)out.close();,Java的序列化與反序列化

14、,/反序列化對象 ObjectInputStream in = new ObjectInputStream(new FileInputStream(D:objectFile.obj);System.out.println(obj1= + (String) in.readObject();System.out.println(obj2= + (Date) in.readObject();Customer obj3 = (Customer) in.readObject();System.out.println(obj3= + obj3);int obj4 = in.readInt();System

15、.out.println(obj4= + obj4);in.close();,Java的序列化與反序列化,ByteArrayOutputStream ot = new ByteArrayOutputStream(); ObjectOutputStream oo = new ObjectOutputStream(ot); oo.writeObject(this); ByteArrayInputStream it = new ByteArrayInputStream(ot.toByteArray(); ObjectInputStream oi = new ObjectInputStream(it)

16、; return (oi.readObject();,Java的序列化與反序列化,最終的原型模式的實例可能是這個樣子: import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable;,3.5 Prototype Pattern (原型模式),3.5 Protot

17、ype Pattern (原型模式),SuppressWarnings(serial) abstract class Clone implements Cloneable,Serializable public Object clone() Object o=new Object(); try o=super.clone(); catch (CloneNotSupportedException e) e.printStackTrace(); return o; abstract public Object deepClone(); ,3.5 Prototype Pattern (原型模式),S

18、uppressWarnings(serial) class myClass extends Clone public int age; Override public Object deepClone() try ByteArrayOutputStream ot = new ByteArrayOutputStream(); ObjectOutputStream oo = new ObjectOutputStream(ot); oo.writeObject(this); ByteArrayInputStream it = new ByteArrayInputStream(ot.toByteArr

19、ay(); ObjectInputStream oi = new ObjectInputStream(it); return (oi.readObject(); catch (IOException e) e.printStackTrace(); return null; catch (ClassNotFoundException e) e.printStackTrace(); return null; ,3.5 Prototype Pattern (原型模式),SuppressWarnings(serial) class person extends Clone public int age

20、; public String name; SuppressWarnings(serial) class student extends Clone public person man; ,3.5 Prototype Pattern (原型模式),public class test public static void main(String args) person p=new person();p.age=20;=zhang; student s=new student(),s1;s.man=p; s1=(student)s.deepClone(); =wang;s1.man.age=30; System.out.println(+=+s.man.age); System.out.println(+=+s1.man.age); ,Prototype Pattern小結(jié),Prototype模式的對象創(chuàng)建方法,具有以下特點:-

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論