




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
第五章原型模式五.一問題地提出五.二原型模式五.三原型復(fù)制具體實現(xiàn)方法五.四應(yīng)用示例五.一問題地提出在計算機程序開發(fā)過程,有時會遇到為一個類創(chuàng)建多個實例地情況,這些實例內(nèi)部成員往往完全相同或有細(xì)微地差異,而且實例地創(chuàng)建開銷比較大或者需要輸入較多參數(shù),如果能通過復(fù)制一個已創(chuàng)建地對象實例來重復(fù)創(chuàng)建多個相同地對象,這就可以大大減少創(chuàng)建對象地開銷,這個時候就需要原型模式。一.淺復(fù)制。五.二原型模式二.深復(fù)制。五.三原型復(fù)制具體實現(xiàn)方法考慮學(xué)生對象復(fù)制問題。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); //從流里讀回來 ByteArrayInputStreambis=newByteArrayInputStream(bos.toByteArray()); ObjectInputStreamois=newObjectInputStream(bis); obj=ois.readObject(); } catch(Exceptione){e.printStackTrace();} returnobj; } }五.四應(yīng)用示例例五-一原型管理器及其應(yīng)用。importjava.util.*;classPrototypeManager{//定義一個Hashtable,用于存儲原型對象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();}}測試類地具體代碼如下所示。importjava.util.*;publicclassTest{ publicstaticvoidmain(String[]args)throwsException{ intm=一零,n=一零; PrototypeManagerpm=PrototypeManager.getPrototypeManager(); PubInfop=newPubInfo("liaoshi","dalian","一一六零八一"); Students=newStudent(); //創(chuàng)建遼師學(xué)生原型對象 s.setInfo(p); pm.addPrototype("liaoshi",s); //加入原形管理器 PubInfop二=newPubInfo("dagong","dalian","一一六零二三"); Students二=newStudent(); //創(chuàng)建大工學(xué)生原型對象 s二.setInfo(p二); pm.addPrototype("dagong",s二); //加入原形管理器
Scannersc=newScanner(System.in); Vector<Student>vec=newVector(); //創(chuàng)建遼師學(xué)生集合 Studentt=(Student)pm.getPrototype("liaoshi");//獲取原型對象 for(inti=零;i<m;i++){ Studentst=(Student)t.clone(); //通過淺復(fù)制創(chuàng)建新學(xué)生對象 st.setName(sc.nextLine()); //輸入并設(shè)置姓名 st.setAge(sc.nextInt()); //輸入并設(shè)置年齡
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 人工全髖關(guān)節(jié)置換護(hù)理
- 2025年硅酮結(jié)構(gòu)密封膠項目立項申請報告
- 早晨 教學(xué)課件
- 教學(xué)一體機使用數(shù)學(xué)課件
- 床鋪課件護(hù)理
- 【公開課】第2課時 有理數(shù)的混合運算 課件+-+2025-2026學(xué)年人教版數(shù)學(xué)七年級上冊
- 電子積木教學(xué)課件
- 新媒體教學(xué)課件
- DB3415-T 65-2023 六安瓜片生態(tài)茶園田間管理指南
- 2025年國家林業(yè)和草原局直屬事業(yè)單位第一批招聘應(yīng)屆畢業(yè)生96人筆試歷年典型考題及考點剖析附帶答案詳解
- 骨盆骨折中醫(yī)護(hù)理常規(guī)
- mil-std-1916抽樣標(biāo)準(zhǔn)(中文版)
- 大學(xué)學(xué)院“十四五”師資隊伍建設(shè)規(guī)劃(2021-2025)
- 鋰電池行業(yè)MES應(yīng)用解決方案
- TCHALPA 0004-2023 民用無人機應(yīng)急救援應(yīng)用專業(yè)操控員合格證考試點管理辦法
- 2023-2024蘇教版七年級數(shù)學(xué)上冊期末試卷
- 英國和美國社區(qū)居家安寧療護(hù)服務(wù)模式及其對我國的啟示
- 橙黃橘綠半甜時 季羨林 史鐵生 汪曾祺等文學(xué)大家
- 極化曲線研究論文
- 2023年呂梁市文水縣社區(qū)工作者招聘考試真題
- 設(shè)計報價單模板
評論
0/150
提交評論