设计模式-软件体系结构-实验4-中南大学-软件学院

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1《软件体系结构》实验报告项目名称结构型设计模式实验专业班级学号姓名实验成绩:批阅教师:年月日2实验4结构型设计模式实验实验学时:2每组人数:1实验类型:3(1:基础性2:综合性3:设计性4:研究性)实验要求:1(1:必修2:选修3:其它)实验类别:3(1:基础2:专业基础3:专业4:其它)一、实验目的熟练使用PowerDesigner和任意一种面向对象编程语言实现几种常见的结构型设计模式,包括适配器模式、组合模式和外观模式,理解每一种设计模式的模式动机,掌握模式结构,学习如何使用代码实现这些模式。二、实验内容1.现有一个接口DataOperation定义了排序方法sort(int[])和查找方法search(int[],int),已知类QuickSort的quickSort(int[])方法实现了快速排序算法,类BinarySearch的binarySearch(int[],int)方法实现了二分查找算法。试使用适配器模式设计一个系统,在不修改源代码的情况下将类QuickSort和类BinarySearch的方法适配到DataOperation接口中。绘制类图并编程实现。(要求实现快速排序和二分查找,使用对象适配器实现)2.WindowsMediaPlayer和RealPlayer是两种常用的媒体播放器,它们的API结构和调用方法存在区别。现在你的应用程序需要支持这两种播放器API,而且在将来可能还需要支持新的媒体播放器,请问如何设计该应用程序?绘制类图并编程模拟实现。3.使用组合模式设计一个杀毒软件(AntiVirus)的框架,该软件既可以对某个文件夹(Folder)杀毒,也可以对某个指定的文件(File)进行杀毒,文件种类包括文本文件TextFile、图片文件ImageFile、视频文件VideoFile。绘制类图并编程模拟实现。4.某教育机构组织结构如下图所示:北京总部教务办公室湖南分校行政办公室长沙教学点湘潭教学点教务办公室行政办公室教务办公室行政办公室教务办公室行政办公室3在该教育机构的OA系统中可以给各级办公室下发公文,试采用组合模式设计该机构的组织结构,绘制相应的类图并编程模拟实现,在客户端代码中模拟下发公文。5.某软件公司为新开发的智能手机控制与管理软件提供了一键备份功能,通过该功能可以将原本存储在手机中的通信录、短信、照片、歌曲等资料一次性全部拷贝到移动存储介质(例如MMC卡或SD卡)中。在实现过程中需要与多个已有的类进行交互,例如通讯录管理类、短信管理类等,为了降低系统的耦合度,试使用外观模式来设计并编程模拟实现该一键备份功能。6.某信息系统需要提供一个数据处理和报表显示模块,该模块可以读取不同类型的文件中的数据并将数据转换成XML格式,然后对数据进行统计分析,最后以报表方式来显示数据。由于该过程需要涉及到多个类,试使用外观模式设计该数据处理和报表显示模块。考虑到有些文件本身已经是XML格式,无须进行格式转换,为了让系统具有更好的扩展性,在系统设计中可以引入抽象外观类。三、实验要求1.结合实例,正确无误地绘制适配器模式、组合模式和外观模式的模式结构图;2.使用任意一种面向对象编程语言实现适配器模式、组合模式和外观模式实例,代码运行正确无误。四、实验步骤1.结合实例,使用PowerDesigner绘制适配器模式实例结构图并用面向对象编程语言实现该模式实例;2.结合实例,使用PowerDesigner绘制适配器模式实例结构图并用面向对象编程语言实现该模式实例;3.结合实例,使用PowerDesigner绘制组合模式实例结构图并用面向对象编程语言实现该模式实例;4.结合实例,使用PowerDesigner绘制组合模式实例结构图并用面向对象编程语言实现该模式实例;5.结合实例,使用PowerDesigner绘制外观模式实例结构图并用面向对象编程语言实现该模式实例;6.结合实例,使用PowerDesigner绘制外观模式实例结构图并用面向对象编程语言实现该模式实例。五、实验结果1.类图:4DataOperation++sort(intiArray[])search(intiArray[],inttarget)...:void:intAdapter--qSortbSearch:QuickSort:BinarySearch+++ConstructorAdapter()sort(intiArray[])search(intiArray[],inttarget):void:intQuickSort+quickSort(intiArray[])...:voidBinarySearch+binarySearch(intiArray[],inttarget):int实现代码:importutil.XMLUtil;publicclassClient{publicstaticvoidmain(String[]args){DataOperationdataOperation=(DataOperation)XMLUtil.getBean();intiArray[]={1,3,2,5,4};booleanresult=dataOperation.search(iArray,9);for(inti=0;i5;i++){System.out.println(iArray[i]);}System.out.println(result);}}publicinterfaceDataOperation{publicvoidsort(int[]iArray);publicbooleansearch(int[]iArray,inttarget);}5publicclassDataOperationAdapterimplementsDataOperation{privateQuickSortqSort;privateBinarySearchbSearch;publicDataOperationAdapter(){qSort=newQuickSort();bSearch=newBinarySearch();}@Overridepublicvoidsort(int[]iArray){qSort.quickSort(iArray);}@Overridepublicbooleansearch(int[]iArray,inttarget){returnbSearch.binarySearch(iArray,target);}}publicclassBinarySearch{publicbooleanbinarySearch(int[]iArray,inttarget){(newQuickSort()).quickSort(iArray);if(bSearch(iArray,target,0,iArray.length-1)==-1){returnfalse;}returntrue;}publicintbSearch(int[]iArray,inttarget,intlow,inthigh){if(lowhigh){6return-1;}intmid=(high+low)/2;if(iArray[mid]==target){returnmid;}elseif(iArray[mid]target){returnbSearch(iArray,target,mid+1,high);}elseif(iArray[mid]target){returnbSearch(iArray,target,low,mid-1);}else{return-1;}}}publicclassQuickSort{publicvoidquickSort(int[]iArray){sort(iArray,0,iArray.length-1);}publicvoidsort(intarr[],intlow,inthigh){intl=low;inth=high;intpovit=arr[low];while(lh){while(lh&&arr[h]=povit)h--;if(lh){inttemp=arr[h];arr[h]=arr[l];arr[l]=temp;7l++;}while(lh&&arr[l]=povit)l++;if(lh){inttemp=arr[h];arr[h]=arr[l];arr[l]=temp;h--;}}//print(arr);//System.out.print(l=+(l+1)+h=+(h+1)+povit=+povit//+\n);if(llow)sort(arr,low,h-1);if(hhigh)sort(arr,l+1,high);}}packageutil;importjava.io.File;importjava.io.IOException;importjavax.xml.parsers.DocumentBuilder;importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.parsers.ParserConfigurationException;importorg.w3c.dom.Document;8importorg.w3c.dom.Node;importorg.w3c.dom.NodeList;importorg.xml.sax.SAXException;publicclassXMLUtil{publicstaticObjectgetBean(){try{//创建DOM文档对象DocumentBuilderFactorydocFactory=DocumentBuilderFactory.newInstance();DocumentBuilderdocBuilder=docFactory.newDocumentBuilder();Documentdocument=docBuilder.parse(newFile(config.xml));//获取包含类名的文本节点NodeListnl=document.getElementsByTagName(className);NodeclassNode=nl.item(0).getFirstChild();StringclassName=classNode.getNodeValue();//通过类名生成实例对象并将其返回@SuppressWarnings(rawtypes)Classclazz=Class.forName(className);Objectobj=clazz.newInstance();returnobj;}catch(ParserConfigurationExceptione){e.printStackTrace();}catch(SAXExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}catch(ClassNotFoundExceptione){e.printStackTrace();}catch(InstantiationExceptione){9e.printStackTrace();}catch(IllegalAccessExceptione){e.printStackTrace();}returnnull;}}?xmlversion=1.0encoding=UTF-8?xml-bodyclassNameDataOperationAdapter/className/xml-body2.类图:WMPlayerAdapter++Constructorplay()WMPlayerAdapter()...:voidPlayer+play()...:voidWindowsMediaPlayer+play()...:voidRealPlayer+play(

1 / 29
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功