




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第五章原型模式五.一問(wèn)題地提出五.二原型模式五.三原型復(fù)制具體實(shí)現(xiàn)方法五.四應(yīng)用示例五.一問(wèn)題地提出在計(jì)算機(jī)程序開(kāi)發(fā)過(guò)程,有時(shí)會(huì)遇到為一個(gè)類創(chuàng)建多個(gè)實(shí)例地情況,這些實(shí)例內(nèi)部成員往往完全相同或有細(xì)微地差異,而且實(shí)例地創(chuàng)建開(kāi)銷比較大或者需要輸入較多參數(shù),如果能通過(guò)復(fù)制一個(gè)已創(chuàng)建地對(duì)象實(shí)例來(lái)重復(fù)創(chuàng)建多個(gè)相同地對(duì)象,這就可以大大減少創(chuàng)建對(duì)象地開(kāi)銷,這個(gè)時(shí)候就需要原型模式。一.淺復(fù)制。五.二原型模式二.深復(fù)制。五.三原型復(fù)制具體實(shí)現(xiàn)方法考慮學(xué)生對(duì)象復(fù)制問(wèn)題。classStudent{ Stringname; //姓名 intage; //年齡 Addressadd; //籍貫信息 Student(Stringna,inta,Addressadd){ =na; age=a; this.add=add; } publicStringgetName(){returnname;} publicvoidsetName(Stringname){=name;} publicintgetAge(){returnage;} publicvoidsetAge(intage){this.age=age;} publicAddressgetAdd(){returnadd;} publicvoidsetAdd(Addressadd){this.add=add;} }classAddress{ Stringpro; //出生省 Stringcity; //出生市 Stringzip; //出生地郵編 publicAddress(Stringp,Stringc,Stringz){ pro=p;city=c;zip=z; } publicStringgetPro(){returnpro;} publicvoidsetPro(Stringpro){=pro;} publicStringgetCity(){returncity;} publicvoidsetCity(Stringcity){this.city=city;} publicStringgetZip(){returnzip;} publicvoidsetZip(Stringzip){this.zip=zip;} }五.三.一構(gòu)造函數(shù)方法(一)淺復(fù)制。 publicclassStudent{ //其它所有代碼同五.三,略 Student(Students){ name=s.getName(); age=s.getAge(); add=s.getAdd(); }}(二)深復(fù)制。classAddress{ //其它所有代碼同五.三,略 publicAddress(Addressadd){ pro=add.getPro(); city=add.getCity(); zip=add.getZip(); }}classStudent{ //其它所有代碼同五.三,略 Student(Students){ name=s.getName(); age=s.getAge(); add=newAddress(s.getAdd()); }}五.三.二利用Cloneable接口方法(一)淺復(fù)制。publicclassStudentimplementsCloneable{ //其它代碼同五.三 protectedObjectclone()throwsCloneNotSupportedException{ returnsuper.clone(); } }(二)深復(fù)制。
classAddressimplementsCloneable{
//其它代碼同五.三
protectedObjectclone()throwsCloneNotSupportedException{
Addresss=(Address)super.clone();
returns;
}
}
classStudentimplementsCloneable{
//其它代碼同五.三
protectedObjectclone()throwsCloneNotSupportedException{
Students=(Student)super.clone();
s.setAdd((Address)add.clone());
returns;
}
}五.三.三利用Serializable序列化接口方法classAddressimplementsSerializable{ //其它代碼同五.三}classStudentimplementsCloneable,Serializable{ //其它代碼同六.三 protectedObjectclone()throwsCloneNotSupportedException{ Objectobj=null; try{ ByteArrayOutputStreambos=newByteArrayOutputStream(); ObjectOutputStreamoos=newObjectOutputStream(bos); oos.writeObject(this); //從流里讀回來(lái) ByteArrayInputStreambis=newByteArrayInputStream(bos.toByteArray()); ObjectInputStreamois=newObjectInputStream(bis); obj=ois.readObject(); } catch(Exceptione){e.printStackTrace();} returnobj; } }五.四應(yīng)用示例例五-一原型管理器及其應(yīng)用。importjava.util.*;classPrototypeManager{//定義一個(gè)Hashtable,用于存儲(chǔ)原型對(duì)象privateHashtableht=newHashtable();privatestaticPrototypeManagerpm=newPrototypeManager();publicvoidaddPrototype(Stringkey,Objectobj){ht.put(key,obj);}publicObjectgetPrototype(Stringkey){returnht.get(key);}publicstaticPrototypeManagergetPrototypeManager(){returnpm;}}例如:以學(xué)生基本信息為例,但內(nèi)容與五.三節(jié)有所不同,基本類如下所示。publicclassStudentimplementsCloneable{ Stringname;//學(xué)生姓名 intage; //年齡 PubInfoinfo; //公信息 publicStringgetName(){returnname;} publicvoidsetName(Stringname){=name;} publicintgetAge(){returnage;} publicvoidsetAge(intage){this.age=age;} publicPubInfogetInfo(){returninfo;} publicvoidsetInfo(PubInfoinfo){=info;} protectedObjectclone()throwsCloneNotSupportedException {returnsuper.clone();} }classPubInfoimplementsCloneable{ Stringcollege; //所在大學(xué) Stringcity; //所在城市 Stringzip; //郵編 publicPubInfo(Stringco,Stringc,Stringz){ college=co;city=c;zip=z; } publicStringgetCollege(){returncollege;} publicvoidsetCollege(Stringcollege){this.college=college;} publicStringgetCity(){returncity;} publicvoidsetCity(Stringcity){this.city=city;} publicStringgetZip(){returnzip;} publicvoidsetZip(Stringzip){this.zip=zip;} protectedObjectclone()throwsCloneNotSupportedException {returnsuper.clone();}}測(cè)試類地具體代碼如下所示。importjava.util.*;publicclassTest{ publicstaticvoidmain(String[]args)throwsException{ intm=一零,n=一零; PrototypeManagerpm=PrototypeManager.getPrototypeManager(); PubInfop=newPubInfo("liaoshi","dalian","一一六零八一"); Students=newStudent(); //創(chuàng)建遼師學(xué)生原型對(duì)象 s.setInfo(p); pm.addPrototype("liaoshi",s); //加入原形管理器 PubInfop二=newPubInfo("dagong","dalian","一一六零二三"); Students二=newStudent(); //創(chuàng)建大工學(xué)生原型對(duì)象 s二.setInfo(p二); pm.addPrototype("dagong",s二); //加入原形管理器
Scannersc=newScanner(System.in); Vector<Student>vec=newVector(); //創(chuàng)建遼師學(xué)生集合 Studentt=(Student)pm.getPrototype("liaoshi");//獲取原型對(duì)象 for(inti=零;i<m;i++){ Studentst=(Student)t.clone(); //通過(guò)淺復(fù)制創(chuàng)建新學(xué)生對(duì)象 st.setName(sc.nextLine()); //輸入并設(shè)置姓名 st.setAge(sc.nextInt()); //輸入并設(shè)置年齡
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 早安正能量測(cè)試題及答案
- 掌握金融科技對(duì)證券行業(yè)的影響試題及答案
- 2025年銀行從業(yè)資格證考試信息反饋機(jī)制試題及答案
- 重點(diǎn)提煉:微生物檢驗(yàn)技師試題及答案
- 2024是項(xiàng)目管理考試的關(guān)鍵年份試題及答案
- 地磚打磨施工方案怎么寫
- 2024年項(xiàng)目管理考試講義試題及答案
- 遠(yuǎn)程項(xiàng)目管理的策略探討試題及答案
- 寧夏擠塑板地面施工方案
- 液壓馬達(dá)的排量控制考核試卷
- 招標(biāo)代理服務(wù)投標(biāo)方案(技術(shù)標(biāo))
- 市政工程施工組織設(shè)計(jì)方案
- 2024年(學(xué)習(xí)強(qiáng)國(guó))思想政治理論知識(shí)考試題庫(kù)與答案
- 《三氣周瑜》兒童故事繪本ppt課件(圖文演講)
- 消防給水及消火栓系統(tǒng)工程驗(yàn)收記
- 鉆孔灌注樁工程結(jié)算關(guān)于充盈系數(shù)的爭(zhēng)議處理及分析(蘇亞金愛(ài)國(guó))
- 本科畢業(yè)設(shè)計(jì)論文霓虹燈PLC控制與監(jiān)控組態(tài)設(shè)計(jì)
- 揚(yáng)塵防治教育培訓(xùn)記錄(共11頁(yè))
- 2020年TDLTE無(wú)線網(wǎng)絡(luò)主設(shè)備功能測(cè)試規(guī)范基本功能分冊(cè)
- JJG 629-2014 多晶X射線衍射儀(原版-高清)
- 畢業(yè)設(shè)計(jì)(論文)印染廢水處理工藝設(shè)計(jì)
評(píng)論
0/150
提交評(píng)論