您好,欢迎访问三七文档
高级语言程序设计实验指导书1一.实验目的1.熟悉UML统一建模语言;2.回顾熟悉迭代器模式、中介者模式、备忘录模式;3.熟悉XML文件的操作;4.熟悉java的反射机制。二.实验内容(1)某教务管理系统中一个班级(Class)包含多个学生(Student),使用Java内置迭代器实现对学生信息的遍历,要求按学生年龄由大到小的次序输出学生信息。用Java实现该过程。(2)使用中介者模式来说明联合国的作用,要求绘制相应的类图并分析每个类的作用(注:可以将联合国定义为抽象中介者类,联合国下属机构如WTO、WFC、WHO等作为具体中介者类,国家可以作为抽象同事类,而将中国、美国、日本、英国等国家作为具体同事类)。(3)改进“用户信息操作撤销”实例,使得系统可以实现多次撤销操作(可以使用集合对象如HashMap、ArrayList等来实现)。三.实验环境PC微机;Windows操作系统;VisualStudio程序集成环境。四.实验内容(1)某教务管理系统中一个班级(Class)包含多个学生(Student),使用Java内置迭代器实现对学生信息的遍历,要求按学生年龄由大到小的次序输出学生信息。用Java实现该过程。Student.javapackage设计模式实验十七周_1;publicclassStudentimplementsComparableStudent{privateStringno;privateStringname;privateintage;privateStringzy;publicStudent(Stringno,Stringname,intage,Stringzy){super();this.no=no;this.name=name;this.age=age;高级语言程序设计实验指导书2this.zy=zy;}//三个返回结果都要写出来publicintcompareTo(Studento){if(this.ageo.age){return-1;}elseif(this.ageo.age){return1;}else{return0;}}@OverridepublicStringtoString(){return姓名:+this.name+.编号:+this.no+.年龄:+this.age+.专业:+this.zy;}}Client.javapackage设计模式实验十七周_1;importjava.util.ArrayList;importjava.util.Collections;importjava.util.List;publicclassClient{publicstaticvoidmain(String[]args){Students1=newStudent(291,张一,20,音乐专业);Students2=newStudent(423,李红,21,计算机专业);Students3=newStudent(211,王文,22,机械专业);Students4=newStudent(445,高龙,19,国贸专业);ListStudentlist=newArrayListStudent();list.add(s1);list.add(s2);list.add(s3);list.add(s4);Collections.sort(list);System.out.println(按照年龄进行排序输出:);for(Studentstu:list){System.out.println(stu.toString());}高级语言程序设计实验指导书3}}UML.java运行结果:(2)使用中介者模式来说明联合国的作用,要求绘制相应的类图并分析每个类的作用(注:可以将联合国定义为抽象中介者类,联合国下属机构如WTO、WFC、WHO等作为具体中介者类,国家可以作为抽象同事类,而将中国、美国、日本、英国等国家作为具体同事类)。UN.javapackage设计模式实验十七周_2;importjava.util.Hashtable;publicabstractclassUN{protectedStringname;protectedHashtableString,Countrycountry;publicStringgetName(){returnname;}高级语言程序设计实验指导书4publicvoidsetName(Stringname){this.name=name;}publicUN(){this.country=newHashtableString,Country();}publicvoidAddCountry(Countryco){if(country==null)return;country.put(co.getName(),co);}publicvoidDelCountry(Stringco){if(country==null)return;country.remove(co);}publicvoidDelCountry(Countryco){if(country==null)return;country.remove(co.getName());}protectedvoidSendMessage(Stringfrom,Stringto,Stringmsg){Countryco=country.get(to);if(co!=null)co.ReceiveMessage(from,msg);elseSystem.out.println(对不起!“+from+”不存在,可能还未加入UN!);}publicvoidReceiveMessage(Stringfrom,Stringto,Stringmsg){SendMessage(from,to,msg);}}Country.javapackage设计模式实验十七周_2;高级语言程序设计实验指导书5publicabstractclassCountry{protectedStringname;protectedUNwto,who,wfc;publicUNgetWto(){returnwto;}publicvoidsetWto(UNwto){this.wto=wto;}publicUNgetWho(){returnwho;}publicvoidsetWho(UNwho){this.who=who;}publicUNgetWfc(){returnwfc;}publicvoidsetWfc(UNwfc){this.wfc=wfc;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicabstractvoidSendMessageByWTO(Stringcou,Stringmsg);publicvoidSendMessageByWHO(Stringcou,Stringmsg){who.SendMessage(this.name,cou,msg);}高级语言程序设计实验指导书6publicvoidSendMessageByWFC(Stringcou,Stringmsg){wfc.SendMessage(this.name,cou,msg);}publicvoidReceiveMessage(Stringcou,Stringmsg){System.out.println(name+收到来自+cou+的消息:“+msg+”);}@Overridepublicbooleanequals(Objectco){Countryc=(Country)co;if(co==null||this.name==null||c.getName()==null)returnfalse;returnname.equals(c.getName());}}WHO.javapackage设计模式实验十七周_2;publicclassWHOextendsUN{publicWHO(){super();this.name=newString(WHO);}}WFC.javapackage设计模式实验十七周_2;publicclassWFCextendsUN{publicWFC(){super();this.name=newString(WFC);}}高级语言程序设计实验指导书7WTO.javapackage设计模式实验十七周_2;publicclassWTOextendsUN{publicWTO(){super();this.name=newString(WTO);}}America.javapackage设计模式实验十七周_2;publicclassAmericaextendsCountry{publicAmerica(){this.name=newString(America);}@OverridepublicvoidSendMessageByWTO(Stringcou,Stringmsg){wto.SendMessage(this.name,cou,msg);}}China.javapackage设计模式实验十七周_2;publicclassChinaextendsCountry{publicChina(){this.name=newString(China);}@OverridepublicvoidSendMessageByWTO(Stringcou,Stringmsg){wto.SendMessage(this.name,cou,msg);}}高级语言程序设计实验指导书8Japan.javapackage设计模式实验十七周_2;publicclassJapanextendsCountry{publicJapan(){this.name=newString(Japan);}@OverridepublicvoidSendMessageByWTO(Stringcou,Stringmsg){wto.SendMessage(this.name,cou,msg);}}Client.javapackage设计模式实验十七周_2;publicclassClient{publicstaticvoidmain(String[]args){UN[]un=newUN[3];//UNwto,who,wfc;un[0]=newWTO();un[1]=newWHO();un[2]=newWFC();Country[]cou=newCountry[3];cou[0]=newChina();cou[1]=newJapan();cou[2]=newAmerica();for(inti=0;iun.length;i++)for(intj=0;jcou.length;j++)un[i].AddCountry(cou[j]);for(inti=0;icou.length;i++){cou[i].setWfc(un[2]);cou[i].setWho(un[1]);高级语言程序设计实验指导书9cou[i].setWto(un[0]);}cou[0].SendMessageByWTO(America,中方坚决反对美方干涉台湾问题);cou[1].SendMessageByWHO(China,通过WHO....);cou[2].SendMessageByWFC(China,通过WFC....);}}UML.java运行结果:(3)改进“用户信息操作撤销”实例,使得系统可以实现多次撤销操作(可以使用集合对象如HashMap、ArrayList等来实现)。UserInfoDTO.javapackage设计模式实验十七周_3;publicclassUserInfoDTO{privateStringaccount;privateStringpassword;privateStringtelNo;高
本文标题:设计模式十七
链接地址:https://www.777doc.com/doc-2143507 .html