云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 郑州大学软件学院设计模式

郑州大学软件学院设计模式

  • 62 次阅读
  • 3 次下载
  • 2026/4/24 5:24:11

NodeList nl = doc.getElementsByTagName

(\

Node

classNode=nl.item(0).getFirstChild(); String

cName=classNode.getNodeValue();

//通过类名生成实例对象并将其返回

Class

c=Class.forName(cName); Object obj=c.newInstance(); return obj; }

catch(Exception e) { e.printStackTrace(); return null; } } }

实例二:日志记录器

抽象工厂模式

实例一:电器工厂

一个电器工厂可以产生多种类型的电器,如海尔工厂可以生产海尔电视机、海尔空调等,TCL工厂可以生产TCL电视机、TCL空调等,相同品牌的电器构成一个产品族,而相同类型的电器构成了一个产品等级结构,现使用抽象工厂模式模拟该场景。 public interface EFactory { public Television produceTelevision(); public AirConditioner produceAirConditioner(); }

public class HaierFactory implements EFactory

{ public Television produceTelevision() { return new HaierTelevision(); } public AirConditioner produceAirConditioner() { return new HairAirConditioner(); } }

public class HaierTelevision implements Television { public void play() { System.out.println(\海尔电视机播放中......\ } }

public class HairAirConditioner implements AirConditioner { public void changeTemperature() { System.out.println(\海尔空调温度改变中......\ } }

public interface Television { public void play(); }

原型模式

实例一:邮件复制(浅克隆)

由于邮件对象包含的内容较多(如发送者、接收者、标题、内容、日期、附件等),某系统中现需要提供一个邮件复制功能,对于已经创建好的邮件对象,可以通过复制的方式创建一个新的邮件对象,如果需要改变某

部分内容,无须修改原始的邮件对象,只需要修改复制后得到的邮件对象即可。使用原型模式设计该系统。在本实例中使用浅克隆实现邮件复制,即复制邮件(Email)的同时不复制附件(Attachment)。 public class Attachment {

public void download() { System.out.println(\下载附件\ } }

public class Client { public static void main(String a[]) { Email email,copyEmail; email=new Email(); copyEmail=(Email)email.clone(); System.out.println(\); System.out.println(email==copyEmail); System.out.println(\==copyEmail.getAttachment?\ System.out.println(email.getAttachment()==copyEmail.getAttachment()); } }

public class Email implements Cloneable { private Attachment attachment=null; public Email() { this.attachment=new Attachment(); }

public Object clone() { Email clone=null; try { clone=(Email)super.clone(); }

catch(CloneNotSupportedException e)

{ System.out.println(\failure!\ } return clone; } public Attachment getAttachment() { return this.attachment; } public void display() { System.out.println(\查看邮件\ } }

实例二:邮件复制(深克隆) 使用深克隆实现邮件复制,即复制邮件的同时复制附件。 import java.io.*;

public class Attachment implements Serializable {

public void download() { System.out.println(\下载附件\ } }

public class Client {

public static void main(String a[]) { Email email,copyEmail=null; email=new Email(); try{

copyEmail=(Email)email.deepClone(); } catch(Exception e) { e.printStackTrace(); } System.out.println(\); System.out.println(email==copyEmail); System.out.println(\==copyEmail.getAttachment?\ System.out.println(email.getAttachment()==copyEmail.getAttachment()); } }

import java.io.*;

public class Email implements Serializable { private Attachment attachment=null; public Email() { this.attachment=new Attachment(); } public Object deepClone() throws IOException, ClassNotFoundException, OptionalDataException { //将对象写入流中 ByteArrayOutputStream bao=new ByteArrayOutputStream(); ObjectOutputStream oos=new ObjectOutputStream(bao); oos.writeObject(this); //将对象从流中取出 ByteArrayInputStream bis=new ByteArrayInputStream(bao.toByteArray()); ObjectInputStream ois=new ObjectInputStream(bis); return(ois.readObject()); } public Attachment getAttachment() { return this.attachment; } public void display() { System.out.println(\查看邮件\ } }

相似对象的复制

class Student implements Cloneable { private String stuName; private String stuSex; private int stuAge; private String stuMajor; private String stuCollege; private String stuUniversity; public Student(String stuName,String stuSex,int stuAge,String stuMajor,String stuCollege,String stuUniversity) { this.stuName=stuName; this.stuSex=stuSex; this.stuAge=stuAge;

this.stuMajor=stuMajor; this.stuCollege=stuCollege; this.stuUniversity=stuUniversity; } public void setStuName(String stuName) { this.stuName = stuName; } public void setStuSex(String stuSex) { this.stuSex = stuSex; } public void setStuAge(int stuAge) { this.stuAge = stuAge; } public void setStuMajor(String stuMajor) { this.stuMajor = stuMajor; } public void setStuCollege(String stuCollege) { this.stuCollege = stuCollege; } public void setStuUniversity(String stuUniversity) { this.stuUniversity = stuUniversity; } public String getStuName() { return (this.stuName); } public String getStuSex() { return (this.stuSex); } public int getStuAge() { return (this.stuAge); }

public String getStuMajor() { return (this.stuMajor); } public String getStuCollege() { return (this.stuCollege); } public String getStuUniversity() { return (this.stuUniversity); } public Student clone() { Student cpStudent=null; try { cpStudent=(Student)super.clone(); } catch(CloneNotSupportedException e) { } return cpStudent; } }

class MainClass { public static void main(String args[]) { Student stu1,stu2,stu3; stu1=new Student(\张无忌\男\软件工程\软件学院\中南大学\//状态相似 //使用原型模式 stu2=stu1.clone(); stu2.setStuName(\杨过\ //使用原型模式

搜索更多关于: 郑州大学软件学院设计模式 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

NodeList nl = doc.getElementsByTagName (\ Node classNode=nl.item(0).getFirstChild(); String cName=classNode.getNodeValue(); //通过类名生成实例对象并将其返回 Class c=Class.forName(cName); Object obj=c.newInstance(); return obj; } catch(Exception e

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com