高级语言程序设计实验指导书1一.实验目的1.熟悉UML统一建模语言;2.回顾熟悉建造模式、原型模式;3.熟悉XML文件的操作;4.熟悉java的反射机制。二.实验内容(1)某软件公司欲开发一个音频和视频播放软件,为了给用户使用提供方便,该播放软件提供了多种界面显示模式,如完整模式、精简模式、记忆模式、网络模式等。在不同的显示模式下主界面的组成元素有所差异,如在完整模式下将显示菜单、播放列表、主窗口、控制条等,在精简模式下只显示主窗口和控制条,而在记忆模式下将显示主窗口、控制条、收藏列表等。现使用建造者模式设计该软件。(2)设计一个客户类Customer,其中客户地址存储在地址类Address中,用浅克隆和深克隆分别实现Customer对象的复制并比较这两种克隆方式的异同。三.实验环境PC微机;Windows操作系统;VisualStudio程序集成环境。四.实验内容(1)某软件公司欲开发一个音频和视频播放软件,为了给用户使用提供方便,该播放软件提供了多种界面显示模式,如完整模式、精简模式、记忆模式、网络模式等。在不同的显示模式下主界面的组成元素有所差异,如在完整模式下将显示菜单、播放列表、主窗口、控制条等,在精简模式下只显示主窗口和控制条,而在记忆模式下将显示主窗口、控制条、收藏列表等。现使用建造者模式设计该软件。ModelBuilder.javapackage设计模式实验十二周_1;publicabstractclassModelBuilder{protectedModelmodel=newModel();publicabstractvoidbuildPlay();publicabstractvoidbuildMenu();publicabstractvoidbuildPlaylist();publicabstractvoidbuildMainWindow();publicabstractvoidbuildControlbar();publicModelgetModel(){returnmodel;}高级语言程序设计实验指导书2}Diector.javapackage设计模式实验十二周_1;publicclassDiector{privateModelBuildermb;publicvoidsetModelBuilder(ModelBuildermb){this.mb=mb;}publicModelconstruct(){mb.buildPlay();mb.buildMenu();mb.buildPlaylist();mb.buildMainWindow();mb.buildControlbar();returnmb.getModel();}}Model.javapackage设计模式实验十二周_1;publicclassModel{privateStringplay;privateStringmenu;privateStringplaylist;privateStringmainWindow;privateStringcontrolbar;publicvoidsetPlay(Stringplay){this.play=play;}publicvoidsetMenu(Stringmenu){this.menu=menu;}publicvoidsetPlaylist(Stringplaylist){this.playlist=playlist;}publicvoidsetMainWindow(StringmainWindow){this.mainWindow=mainWindow;高级语言程序设计实验指导书3}publicvoidsetControlbar(Stringcontrolbar){this.controlbar=controlbar;}publicStringgetPlay(){return(this.play);}publicStringgetMenu(){return(this.menu);}publicStringgetPlaylist(){return(this.playlist);}publicStringgetMainWindow(){return(this.mainWindow);}publicStringgetControlbar(){return(this.controlbar);}}CompleteModelBuilder.javapackage设计模式实验十二周_1;publicclassCompleteModelBuilderextendsModelBuilder{publicvoidbuildPlay(){model.setPlay(完整模式:);}publicvoidbuildMenu(){model.setMenu(显示菜单);}publicvoidbuildPlaylist(){model.setPlaylist(显示播放列表);}publicvoidbuildMainWindow(){model.setMainWindow(显示主窗口);}publicvoidbuildControlbar(){model.setControlbar(显示控制条);}高级语言程序设计实验指导书4}MemoryModelBuilder.javapackage设计模式实验十二周_1;publicclassMemoryModelBuilderextendsModelBuilder{publicvoidbuildPlay(){model.setPlay(记忆模式:);}publicvoidbuildMenu(){model.setMenu(不显示菜单);}publicvoidbuildPlaylist(){model.setPlaylist(显示播放列表);}publicvoidbuildMainWindow(){model.setMainWindow(显示主窗口);}publicvoidbuildControlbar(){model.setControlbar(显示控制条);}}NetworkModelBuilder.javapackage设计模式实验十二周_1;publicclassNetworkModelBuilderextendsModelBuilder{publicvoidbuildPlay(){model.setPlay(网络模式:);}publicvoidbuildMenu(){model.setMenu(显示菜单);}publicvoidbuildPlaylist(){model.setPlaylist(不显示播放列表);}publicvoidbuildMainWindow(){model.setMainWindow(显示主窗口);高级语言程序设计实验指导书5}publicvoidbuildControlbar(){model.setControlbar(显示控制条);}}ShortModelBuilder.javapackage设计模式实验十二周_1;publicclassShortModelBuilderextendsModelBuilder{publicvoidbuildPlay(){model.setPlay(精简模式:);}publicvoidbuildMenu(){model.setMenu(显示菜单);}publicvoidbuildPlaylist(){model.setPlaylist(不显示播放列表);}publicvoidbuildMainWindow(){model.setMainWindow(不显示主窗口);}publicvoidbuildControlbar(){model.setControlbar(显示控制条);}}Client.javapackage设计模式实验十二周_1;publicclassClient{publicstaticvoidmain(Stringargs[]){ModelBuildermb=(ModelBuilder)XMLUtil.getBean();Diectordiector=newDiector();diector.setModelBuilder(mb);Modelmodel=diector.construct();System.out.println(model.getPlay());高级语言程序设计实验指导书6System.out.println(model.getMenu());System.out.println(model.getPlaylist());System.out.println(model.getMainWindow());System.out.println(model.getControlbar());}}XMLUtil.javapackage设计模式实验十二周_1;importjavax.xml.parsers.*;importorg.w3c.dom.*;importjava.io.*;publicclassXMLUtil{publicstaticObjectgetBean(){try{DocumentBuilderFactorydFactory=DocumentBuilderFactory.newInstance();DocumentBuilderbuilder=dFactory.newDocumentBuilder();Documentdoc;doc=builder.parse(newFile(configModel.xml));NodeListnl=doc.getElementsByTagName(classname);NodeclassNode=nl.item(0).getFirstChild();StringcName=classNode.getNodeValue();Classc=Class.forName(cName);Objectobj=c.newInstance();returnobj;}catch(Exceptione){e.printStackTrace();returnnull;}}}高级语言程序设计实验指导书7configModel.xml?xmlversion=1.0?configclassname设计模式实验十二周_1.MemoryModelBuilder/classname/configUML_1运行结果:(2)设计一个客户类Customer,其中客户地址存储在地址类Address中,用浅克隆和深克隆分别实现Customer对象的复制并比较这两种克隆方式的异同。浅克隆Client.java高级语言程序设计实验指导书8package设计模式实验十二周_2_1;publicclassClient{publicstaticvoidmain(String[]args){Addressaddress=newAddress(东北,daming@163.com,13306446609);Customerxiaoming=newCustomer(001,小明,13,address);Customerxiaohong=xiaoming.clone();xiaohong.setName(小红);xiaohong.setId(002);xiaoming.show();System.out.println(----------);xiaohong.show();System.out.println(----------);System.out.println(xiaoming.equals(xiaohong));System.out.println(xiaoming.getAddr().equals(xiaohong.getAddr()));}}Address.javapackage设计模式实验十二周_2_1;publicclassAddress{private