设计模式原型模式_第1页
设计模式原型模式_第2页
设计模式原型模式_第3页
设计模式原型模式_第4页
设计模式原型模式_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第五章原型模式五.一问题地提出五.二原型模式五.三原型复制具体实现方法五.四应用示例五.一问题地提出在计算机程序开发过程,有时会遇到为一个类创建多个实例地情况,这些实例内部成员往往完全相同或有细微地差异,而且实例地创建开销比较大或者需要输入较多参数,如果能通过复制一个已创建地对象实例来重复创建多个相同地对象,这就可以大大减少创建对象地开销,这个时候就需要原型模式。一.浅复制。五.二原型模式二.深复制。五.三原型复制具体实现方法考虑学生对象复制问题。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;} }五.三.一构造函数方法(一)浅复制。 publicclassStudent{ //其它所有代码同五.三,略 Student(Students){ name=s.getName(); age=s.getAge(); add=s.getAdd(); }}(二)深复制。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接口方法(一)浅复制。publicclassStudentimplementsCloneable{ //其它代码同五.三 protectedObjectclone()throwsCloneNotSupportedException{ returnsuper.clone(); } }(二)深复制。

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; } }五.四应用示例例五-一原型管理器及其应用。importjava.util.*;classPrototypeManager{//定义一个Hashtable,用于存储原型对象privateHashtableht=newHashtable();privatestaticPrototypeManagerpm=newPrototypeManager();publicvoidaddPrototype(Stringkey,Objectobj){ht.put(key,obj);}publicObjectgetPrototype(Stringkey){returnht.get(key);}publicstaticPrototypeManagergetPrototypeManager(){returnpm;}}例如:以学生基本信息为例,但内容与五.三节有所不同,基本类如下所示。publicclassStudentimplementsCloneable{ Stringname;//学生姓名 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; //所在大学 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(); //创建辽师学生原型对象 s.setInfo(p); pm.addPrototype("liaoshi",s); //加入原形管理器 PubInfop二=newPubInfo("dagong","dalian","一一六零二三"); Students二=newStudent(); //创建大工学生原型对象 s二.setInfo(p二); pm.addPrototype("dagong",s二); //加入原形管理器

Scannersc=newScanner(System.in); Vector<Student>vec=newVector(); //创建辽师学生集合 Studentt=(Student)pm.getPrototype("liaoshi");//获取原型对象 for(inti=零;i<m;i++){ Studentst=(Student)t.clone(); //通过浅复制创建新学生对象 st.setName(sc.nextLine()); //输入并设置姓名 st.setAge(sc.nextInt()); //输入并设置年龄

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论