设计模式十七_第1页
设计模式十七_第2页
设计模式十七_第3页
设计模式十七_第4页
设计模式十七_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

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

文档简介

1、高级语言程序设计实验指导书一实验目的1. 熟悉UML统一建模语言;2. 回顾熟悉迭代器模式、中介者模式、备忘录模式;3. 熟悉XML文件的操作;4. 熟悉java的反射机制。二实验内容(1)某教务管理系统中一个班级(Class)包含多个学生(Student),使用Java内置迭代器实现对学生信息的遍历,要求按学生年龄由大到小的次序输出学生信息。用Java实现该过程。(2)使用中介者模式来说明联合国的作用,要求绘制相应的类图并分析每个类的作用(注:可以将联合国定义为抽象中介者类,联合国下属机构如WTO、WFC、WHO等作为具体中介者类,国家可以作为抽象同事类,而将中国、美国、日本、英国等国家作为

2、具体同事类)。(3)改进“用户信息操作撤销”实例,使得系统可以实现多次撤销操作(可以使用集合对象如HashMap、ArrayList等来实现)。三实验环境PC微机;Windows 操作系统;Visual Studio 程序集成环境。4 实验内容(1)某教务管理系统中一个班级(Class)包含多个学生(Student),使用Java内置迭代器实现对学生信息的遍历,要求按学生年龄由大到小的次序输出学生信息。用Java实现该过程。Student.javapackage 设计模式实验十七周_1;public class Student implements Comparable<Student&

3、gt; private String no; private String name; private int age; private String zy; public Student(String no, String name, int age, String zy) super(); this.no = no; = name; this.age = age; this.zy = zy; / 三个返回结果都要写出来 public int compareTo(Student o) if(this.age > o.age) return -1; else if(t

4、his.age < o.age) return 1; else return 0; Override public String toString() return "姓名: " + + ". 编号: " + this.no + ". 年龄: " + this.age + ". 专业: " + this.zy; Client.javapackage 设计模式实验十七周_1;import java.util.ArrayList;import java.util.Collections;imp

5、ort java.util.List;public class Client public static void main(String args) Student s1 = new Student("291", "张一", 20, "音乐专业"); Student s2 = new Student("423", "李红", 21, "计算机专业"); Student s3 = new Student("211", "王文", 22,

6、 "机械专业"); Student s4 = new Student("445", "高龙", 19, "国贸专业"); List<Student> list = new ArrayList<Student>(); list.add(s1); list.add(s2); list.add(s3); list.add(s4); Collections.sort(list); System.out.println("按照年龄进行排序输出: "); for(Student st

7、u : list) System.out.println(stu.toString(); UML.java 运行结果:(2)使用中介者模式来说明联合国的作用,要求绘制相应的类图并分析每个类的作用(注:可以将联合国定义为抽象中介者类,联合国下属机构如WTO、WFC、WHO等作为具体中介者类,国家可以作为抽象同事类,而将中国、美国、日本、英国等国家作为具体同事类)。UN.javapackage 设计模式实验十七周_2;import java.util.Hashtable;public abstract class UN protected String name;protected Hashtab

8、le<String, Country> country;public String getName() return name;public void setName(String name) = name;public UN() this.country = new Hashtable<String, Country>();public void AddCountry(Country co) if (country = null)return;country.put(co.getName(), co);public void DelCountry(

9、String co) if (country = null)return;country.remove(co);public void DelCountry(Country co) if (country = null)return;country.remove(co.getName();protected void SendMessage(String from, String to, String msg) Country co = country.get(to);if (co != null)co.ReceiveMessage(from, msg);elseSystem.out.prin

10、tln("对不起!“" + from + "”不存在,可能还未加入UN!");public void ReceiveMessage(String from, String to, String msg) SendMessage(from, to, msg);Country.javapackage 设计模式实验十七周_2;public abstract class Country protected String name;protected UN wto, who, wfc;public UN getWto() return wto;public voi

11、d setWto(UN wto) this.wto = wto;public UN getWho() return who;public void setWho(UN who) this.who = who;public UN getWfc() return wfc;public void setWfc(UN wfc) this.wfc = wfc;public String getName() return name;public void setName(String name) = name;public abstract void SendMessageByWTO(

12、String cou, String msg);public void SendMessageByWHO(String cou, String msg) who.SendMessage(, cou, msg);public void SendMessageByWFC(String cou, String msg) wfc.SendMessage(, cou, msg);public void ReceiveMessage(String cou, String msg) System.out.println(name + "收到来自" +

13、cou + "的消息:“" + msg + "”");Overridepublic boolean equals(Object co) Country c = (Country) co;if (co = null | = null | c.getName() = null)return false;return name.equals(c.getName();WHO.javapackage 设计模式实验十七周_2;public class WHO extends UN public WHO()super();=new

14、 String("WHO");WFC.javapackage 设计模式实验十七周_2;public class WFC extends UN public WFC() super(); = new String("WFC");WTO.javapackage 设计模式实验十七周_2;public class WTO extends UN public WTO() super(); = new String("WTO");America.javapackage 设计模式实验十七周_2;public cl

15、ass America extends Country public America()=new String ("America");Overridepublic void SendMessageByWTO(String cou, String msg) wto.SendMessage(, cou, msg);China.javapackage 设计模式实验十七周_2;public class China extends Country public China()=new String("China&quo

16、t;);Overridepublic void SendMessageByWTO(String cou, String msg) wto.SendMessage(, cou, msg);Japan.javapackage 设计模式实验十七周_2;public class Japan extends Country public Japan()=new String ("Japan");Overridepublic void SendMessageByWTO(String cou, String msg) wto.SendMessage(t

17、, cou, msg);Client.javapackage 设计模式实验十七周_2;public class Client public static void main(String args) UN un=new UN3;/UN wto,who,wfc;un0=new WTO();un1=new WHO();un2=new WFC();Country cou=new Country3;cou0=new China();cou1=new Japan();cou2=new America();for(int i=0;i<un.length;i+)for(int j=0;

18、j<cou.length;j+)uni.AddCountry(couj);for(int i=0;i<cou.length;i+)coui.setWfc(un2);coui.setWho(un1);coui.setWto(un0);cou0.SendMessageByWTO("America", "中方坚决反对美方干涉台湾问题");cou1.SendMessageByWHO("China","通过WHO.");cou2.SendMessageByWFC("China", "

19、通过WFC.");UML.java运行结果:(3)改进“用户信息操作撤销”实例,使得系统可以实现多次撤销操作(可以使用集合对象如HashMap、ArrayList等来实现)。UserInfoDTO.javapackage 设计模式实验十七周_3;public class UserInfoDTO private String account; private String password; private String telNo; public String getAccount() return account; public void setAccount(String acc

20、ount) this.account=account; public String getPassword() return password; public void setPassword(String password) this.password=password; public String getTelNo() return telNo; public void setTelNo(String telNo) this.telNo=telNo; public Memento saveMemento() return new Memento(account,password,telNo

21、); public void restoreMemento(Memento memento) this.account=memento.getAccount(); this.password=memento.getPassword(); this.telNo=memento.getTelNo(); public void show() System.out.println("Account:" + this.account); System.out.println("Password:" + this.password); System.out.prin

22、tln("TelNo:" + this.telNo); Caretaker.javapackage 设计模式实验十七周_3;public class Caretaker private Memento memento; public Memento getMemento() return memento; public void setMemento(Memento memento) this.memento=memento; Memento.javapackage 设计模式实验十七周_3;class Memento private String account; priv

23、ate String password; private String telNo; public Memento(String account,String password,String telNo) this.account=account; this.password=password; this.telNo=telNo; public String getAccount() return account; public void setAccount(String account) this.account=account; public String getPassword() r

24、eturn password; public void setPassword(String password) this.password=password; public String getTelNo() return telNo; public void setTelNo(String telNo) this.telNo=telNo; Client.javapackage 设计模式实验十七周_3;import 设计模式实验十七周_3.UserInfoDTO;import 设计模式实验十七周_3.Caretaker;public class Client public static void main(String a) UserInfoDTO user=new UserInfoDTO(); Caretaker c=new Caretaker(); Caretaker d=new Caretaker(); user.setAccount("zhangsan"); user.setPassword("123456

温馨提示

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

评论

0/150

提交评论