设计模式上机实验一实验报告_第1页
设计模式上机实验一实验报告_第2页
设计模式上机实验一实验报告_第3页
设计模式上机实验一实验报告_第4页
设计模式上机实验一实验报告_第5页
已阅读5页,还剩37页未读 继续免费阅读

下载本文档

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

文档简介

设计模式试验一实验报告书专业班级软件0703学号姓名吉亚云指导老师刘伟时间3月27日中南大学软件学院试验一设计模式上机试验一一、试验目标熟练使用PowerDesigner绘制类图,结构系统静态结构模型;使用PowerDesigner和任意一个面向对象编程语言实现几个常见设计模式,包含简单工厂模式、工厂方法模式、抽象工厂模式、原型模式、单例模式、适配器模式、桥接模式和组合模式。二、试验要求1.使用PowerDesigner绘制标准UML类图;2.正确无误绘制简单工厂模式、工厂方法模式、抽象工厂模式、原型模式、单例模式、适配器模式、桥接模式和组合模式模式结构图;3.使用任意一个面向对象编程语言实现简单工厂模式、工厂方法模式、抽象工厂模式、原型模式、单例模式、适配器模式、桥接模式和组合模式,代码运行正确无误。三、试验步骤1.熟悉PowerDesigner11惯用功效;2.使用PowerDesigner绘制类图;3.使用PowerDesigner绘制简单工厂模式结构图并用面向对象编程语言实现该模式;4.使用PowerDesigner绘制工厂方法模式结构图并用面向对象编程语言实现该模式;5.使用PowerDesigner绘制抽象工厂模式结构图并用面向对象编程语言实现该模式;6.使用PowerDesigner绘制原型模式结构图并用面向对象编程语言实现该模式;7.使用PowerDesigner绘制单例模式结构图并用面向对象编程语言实现该模式;8.使用PowerDesigner绘制适配器模式结构图并用面向对象编程语言实现该模式;9.使用PowerDesigner绘制桥接模式结构图并用面向对象编程语言实现该模式;10.使用PowerDesigner绘制组合模式结构图并用面向对象编程语言实现该模式。四、试验汇报要求1.绘制实例场景类图;2.提供简单工厂模式结构图及实当代码;3.提供工厂方法模式结构图及实当代码;4.提供抽象工厂模式结构图及实当代码;5.提供原型模式结构图及实当代码;6.提供单例模式结构图及实当代码;7.提供适配器模式结构图及实当代码;8.提供桥接模式结构图及实当代码;9.提供组合模式结构图及实当代码。五、试验结果使用PowerDesigner绘制类图;用PowerDesigner和任意一个面向对象编程语言实现简单工厂模式、工厂方法模式、抽象工厂模式、原型模式、单例模式、适配器模式、桥接模式和组合模式,包含依照实例绘制模式结构图、编写模式实当代码,运行并测试模式实例代码。(1)类图某音像图书出租企业欲开发一个音像图书租赁信息系统,处理日常音像图书租借业务。该系统将统计全部图书信息、唱碟信息、用户信息和用户租借信息。现决定采取面向对象分析和设计方法来开发此系统,经过初步分析,设计人员定义了以下初始实体类:●Book(图书)类属性:no(编号)、title(标题)、author(作者)、isbn(书号)、press(出版社)、price(价格)。方法:每个属性get()方法和set()方法。●Disc(唱碟)类属性:no(编号)、title(标题)、singer(歌手)、producer(制作企业)、price(价格)。方法:每个属性get()方法和set()方法。●Customer(客户)类属性:cno(客户编号)、name(姓名)、address(地址)、tel(电话)。方法:每个属性get()方法和set()方法。●LoanItem(可租项目)类统计Book和Disc共有属性和方法。●RentHistory(租借历史统计)类属性:rno(历史统计编号)、hireTime(出租时间)、returnTime(偿还时间)、cno(客户编号)、no(租借图书或唱碟编号)、deposit(押金金额)。方法:每个属性get()方法和set()方法。在深入分析之后,需要在LoanItem类中定义一个集合来存放每一个图书或唱碟全部租借历史统计。依照以上信息,绘制对应实体类类图。(2)简单工厂模式使用简单工厂模式设计一个能够创建不一样几何形状(Shape)绘图工具类,如可创建圆形(Circle)、方形(Rectangle)和三角形(Triangle)对象,每个几何图形都要有绘制draw()和擦除erase()两个方法,要求在绘制不支持几何图形时,提醒一个UnsupportedShapeException,绘制类图并编程实现。//抽象图形interfaceShape{ voiddraw(); voiderase();}classCircleimplementsShape{ publicvoiddraw() { System.out.println("Circledrawing!"); } publicvoiderase() { System.out.println("Circleeraseing!"); } }classRectangleimplementsShape{ publicvoiddraw() { System.out.println("Rectangledrawing!"); } publicvoiderase() { System.out.println("Rectangleeraseing!"); } }classTriangleimplementsShape{ publicvoiddraw() { System.out.println("Triangledrawing!"); } publicvoiderase() { System.out.println("Triangleeraseing!"); } }//工厂classShapeFactory{ //工厂方法factorymethod publicstaticShapeproduceShape(StringshapeName)throwsUnsupportedShapeException { if(shapeName.equalsIgnoreCase("circle")) { returnnewCircle(); } elseif(shapeName.equalsIgnoreCase("rectangle")) { returnnewRectangle(); } elseif(shapeName.equalsIgnoreCase("triangle")) { returnnewTriangle(); } else { thrownewUnsupportedShapeException(); } }}classUnsupportedShapeExceptionextendsException{ publicStringtoString(){ return"绘制不支持该几何图形!"; } }classShapeClient{ publicstaticvoidmain(Stringa[]) { Shapecircle=null; try{ circle=ShapeFduceShape("circle"); }catch(UnsupportedShapeExceptione){ e.printStackTrace(); } circle.draw(); circle.erase(); Shapetriangle=null; try{ triangle=ShapeFduceShape("triangle"); }catch(UnsupportedShapeExceptione){ e.printStackTrace(); } triangle.draw(); triangle.erase(); }}(3)工厂方法模式海尔工厂(Haier)生产海尔空调(HaierAirCondition),美工厂(Midea)生产美空调(MideaAirCondition)。使用工厂方法模式描述该场景,绘制类图并编程实现。interfaceAirCondition{ voidwork();}interfaceAirConditionFactory{ AirConditionproduceAirCondition();}classHaierAirConditionimplementsAirCondition{ publicvoidwork() { System.out.println("HaierAirConditionworking!"); }}classMideaAirConditionimplementsAirCondition{ publicvoidwork() { System.out.println("MideaAirConditionworking!"); }}classHaierAirConditionFactoryimplementsAirConditionFactory{ publicHaierAirConditionproduceAirCondition() { System.out.println("HaierAirConditionproduced!"); returnnew HaierAirCondition(); }}classMideaAirConditionFactoryimplementsAirConditionFactory{ publicMideaAirConditionproduceAirCondition() { System.out.println("MideaAirConditionproduced!"); returnnew MideaAirCondition(); }}classAirConditionClient{ publicstaticvoidmain(Stringa[]) { AirConditionFactorycf; AirConditionairCondition;cf=(AirConditionFactory)XMLUtil.getBean();airCondition=duceAirCondition();airCondition.work(); }}(4)抽象工厂模式电脑配件生产工厂生产内存、CPU等硬件设备,这些内存、CPU品牌、型号并不一定相同,依照下面“产品等级结构-产品族”示意图,使用抽象工厂模式实现电脑配件生产过程并绘制对应类图,绘制类图并编程实现。interfaceCPU{ voiddiscribe();}interfaceRAM{ voiddiscribe();}classPcCPUimplementsCPU{ publicvoiddiscribe() { System.out.println("PcCPU"); }}classMacCPUimplementsCPU{ publicvoiddiscribe() { System.out.println("MacCPU"); }}classPcRAMimplementsRAM{ publicvoiddiscribe() { System.out.println("PcRAM"); }}classMacRAMimplementsRAM{ publicvoiddiscribe() { System.out.println("MacRAM"); }}interfaceComputerPartsFactory{ CPUproduceCPU(); RAMproduceRAM();}classPcFactoryimplementsComputerPartsFactory{ publicPcCPUproduceCPU(){ System.out.println("PcCPUproduced!"); returnnewPcCPU(); } publicPcRAMproduceRAM(){ System.out.println("PcRAMproduced!"); returnnewPcRAM(); }}classMacFactoryimplementsComputerPartsFactory{ publicMacCPUproduceCPU(){ System.out.println("MacCPUproduced!"); returnnewMacCPU(); } publicMacRAMproduceRAM(){ System.out.println("MacRAMproduced!"); returnnewMacRAM(); }}classComputerPartsClient{ publicstaticvoidmain(Stringa[]) { ComputerPartsFactoryfactory; CPUcpu; RAMram; factory=(ComputerPartsFactory)XMLUtil.getBean(); cpu=duceCPU(); cpu.discribe();ram=duceRAM();ram.discribe(); }}(5)原型模式设计一个客户类Customer,其中客户地址存放在地址类Address中,用浅克隆和深克隆分别实现Customer对象复制并比较这两种克隆方式异同。绘制类图并编程实现。classAddressimplementsSerializable{publicvoiddisplayAddress(){ System.out.println("address...");}}//浅克隆Customer类classCustomerimplementsCloneable{ privateAddressaddress=null; publicAddressgetAddress(){ returnaddress; } publicvoidsetAddress(Addressaddress){ this.address=address; } publicObjectclone() { Customerclone=null; try { clone=(Customer)super.clone(); }catch(CloneNotSupportedExceptione){ System.out.println("Clonefailure!");} returnclone; } }//深克隆Customer类classCustomerDeepCloneimplementsCloneable,Serializable{ privateAddressaddress=null; publicAddressgetAddress(){ returnaddress; } publicvoidsetAddress(Addressaddress){ this.address=address; } publicObjectclone() { Customerclone=null; try { //使用流来进行处理 ByteArrayOutputStreambao=newByteArrayOutputStream(); ObjectOutputStreamoos=newObjectOutputStream(bao); oos.writeObject(this); ByteArrayInputStreambis=newByteArrayInputStream(bao.toByteArray()); ObjectInputStreamois=newObjectInputStream(bis); return(ois.readObject()); }catch(IOExceptione){ e.printStackTrace(); }catch(ClassNotFoundExceptione){ e.printStackTrace(); } returnclone; }}classCustomerClient{ publicstaticvoidmain(Stringa[]) { Customercustomer=newCustomer(); customer.setAddress(newAddress()); CustomercopyCustomer=(Customer)customer.clone(); System.out.println("customer==copyCustomer?"); System.out.println(customer==copyCustomer); System.out.println("customer.getAddress==copyCustomer.getAddress?"); System.out.println(customer.getAddress()==copyCustomer.getAddress()); CustomerDeepClonecustomerDeepClone=newCustomerDeepClone(); customerDeepClone.setAddress(newAddress()); CustomerDeepClonecopyCustomerDeepClone=(CustomerDeepClone)customerDeepClone.clone(); System.out.println("customerDeepClone==copyCustomerDeepClone?"); System.out.println(customerDeepClone==copyCustomerDeepClone); System.out.println("customerDeepClone.getAddress==copyCustomerDeepClone.getAddress?"); System.out.println(customerDeepClone.getAddress()==copyCustomerDeepClone.getAddress()); }}运行结果:(6)单例模式用懒汉式单例实现在某系统运行时,其登录界面类LoginForm只能够弹出一个,假如第二次实例化该类则提醒“程序已运行”。绘制类图并编程实现。classLoginFormextendsJFrame{ privatestaticLoginFormuniqueInstance=null; privateLoginForm() { //设置标题 this.setTitle("LoginForm"); this.setSize(400,300); //设置关闭按钮事件 this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //设置是否可见 this.setVisible(true); } publicstaticLoginFormgetInstance() { if(uniqueInstance==null) { uniqueInstance=newLoginForm(); }else{ JOptionPane.showMessageDialog(null,"LoginFrame已经在运行!"); } returnuniqueInstance; }}classLoginFormClient{ publicstaticvoidmain(Stringa[]) { LoginForms1=null,s2=null; s1=LoginForm.getInstance(); s2=LoginForm.getInstance(); System.out.println("s1==s2?"); System.out.println(s1==s2); }}运行结果:(7)适配器模式现有一个接口DataOperation定义了排序方法sort(int[])和查找方法search(int[],int),已知类QuickSortquickSort(int[])方法实现了快速排序算法,类BinarySearchbinarySearch(int[],int)方法实现了二分查找算法。现使用适配器模式设计一个系统,在不修改源代码情况下将类QuickSort和类BinarySearch方法适配到DataOperation接口中。绘制类图并编程实现。(要求实现快速排序和二分查找)interfaceDataOperation{ voidsort(int[]list); intsearch(int[]list,intnumber);}classQuickSort{ publicint[]quickSort(int[]num){ returnsort(num,0,num.length-1); } privateint[]sort(int[]num,intleft,intright){ if(left<right){ //将第一个数作为参考轴 ints=num[left]; inti=left; intj=right+1; while(true){ //从左向右找,直到找到比S大数 while(i+1<num.length&&num[++i]<s); //从右向左找,直到找到比S小数 while(j-1>-1&&num[--j]>s); //此时假如i>=j,则说明已交叉,跳出该永久循环 if(i>=j){ break; } //不然假如i<=j,则交换下标为i和下标为j两元素值 swap(num,i,j); } //比把S小数放到下标为left处 num[left]=num[j]; //最终将轴S置于比它小和比它大两组数之间 num[j]=s; //对S左右两侧分别再次使用快速排序 sort(num,left,j-1); sort(num,j+1,right); } returnnum; } privatevoidswap(int[]num,inti,intj){ intt; t=num[i]; num[i]=num[j]; num[j]=t; }}classBinarySearch{ publicintbinarySearch(int[]num,intx){ intlow=0; inthigh=num.length-1; while(low<=high){ //此时抛出ArrayIndexOutOfBoundsException异常.. intmid=low+(high-low)/2; intmidVal=num[mid]; if(x>midVal){ low=mid+1; }elseif(x<midVal){ high=mid-1; }else{ returnmid; } } return-1;//没找到元素x }}classDataOperationAdapterimplementsDataOperation{ QuickSortquickSort=newQuickSort(); BinarySearchbinarySearch=newBinarySearch(); @Override publicintsearch(int[]list,intnumber){ returnbinarySearch.binarySearch(list,number); } @Override publicvoidsort(int[]list){ quickSort.quickSort(list); } }classDataOperationClient{ publicstaticvoidmain(Stringa[]) { DataOperationoperation=(DataOperation)XMLUtil.getBean(); int[]list={1,3,56,23,54,86,43,57,88,56,82,90}; System.out.println("Listbeforesort:"); for(inti=0;i<list.length;i++){ System.out.print(list[i]+""); } operation.sort(list); System.out.println("\nListaftersort:"); for(inti=0;i<list.length;i++){ System.out.print(list[i]+""); } intnumber=43; intindex=operation.search(list,number); System.out.println("\nSearchfor:"+number+",Positionis:"+index); }}运行结果:(8)桥接模式空客(Airbus)、波音(Boeing)和麦道(McDonnell-Douglas)都是飞机制造商,它们都生产载客飞机(PassengerPlane)和载货飞机(CargoPlane)。现在需要设计一个系统,描述这些飞机制造商以及它们所制造飞机种类,绘制类图并编程实现。abstractclassAirPlaneManufacturer{ privateAirPlaneairPlane; publicvoidsetAirPlane(AirPlanenewAirPlane){ this.airPlane=newAirPlane; } abstractvoidproduce();}classAirbusextendsAirPlaneManufacturer{ voidproduce(){ System.out.println("Airbus企业制造"); }}classBoeingextendsAirPlaneManufacturer{ voidproduce(){ System.out.println("Boeing企业制造"); }}classMcDonnellDouglasextendsAirPlaneManufacturer{ voidproduce(){ System.out.println("McDonnell-Douglas企业制造"); }}interfaceAirPlane{ voidfly();}classPassengerPlaneimplementsAirPlane{ publicvoidfly(){ System.out.println("客机正在飞行!"); }}classCargoPlaneimplementsAirPlane{ publicvoidfly(){ System.out.println("货机正在飞行!"); }}classBridgePatternClient{ publicstaticvoidmain(Stringa[]) { AirPlaneairPlane; airPlane=(AirPlane)XMLUtilPen.getBean("plane"); AirPlaneManufacturerplaneFactory; planeFactory=(AirPlaneManufacturer)XMLUtilPen.getBean("factory"); planeFactory.setAirPlane(airPlane); planeFduce(); airPlane.fly(); }}运行结果:(9)组合模式使用组合模式设计一个杀毒软件(AntiVirus)框架,该软件既能够对某个文件夹(Folder)杀毒,也能够对某个指定文件(File)进行杀毒,文件种类包含文本文件TextFile、图片文件ImageFile、音频视频文件MediaFile。绘制类图并编程实现。interfaceAbstractElement{ publicvoiddisplay();}classImageFileimplementsAbstractElement{ privateStringfilename; publicImageFile(Stringfilename) { this.filename=filename; } publicvoiddisplay() { System.out.println("Imagefile"+filename+"iskillingvirus!"); }}classTextFileimplementsAbstractElement{ privateStringfilename; publicTextFile(Stringfilename) { this.filename=filename; } publicvoiddisplay() { System.out.println("Textfile"+filename+"iskillingvirus!"); }}classMediaFileimplementsAbstractElement{ privateStringfilename; publicMediaFile(Stringfilename) { thi

温馨提示

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

评论

0/150

提交评论