设计模式实验四Word下载.docx
《设计模式实验四Word下载.docx》由会员分享,可在线阅读,更多相关《设计模式实验四Word下载.docx(20页珍藏版)》请在冰豆网上搜索。
试使用适配器模式设计一个系统,在不修改源代码的情况下将类QuickSort和类BinarySearch的方法适配到DataOperation接口中。
绘
制类图并编程实现。
(要求实现快速排序和二分查找,使用对象适配器实现)
2.WindowsMediaPlayer和RealPlayer是两种常用的媒体播放器,它们的API结构和调
用方法存在区别。
现在你的应用程序需要支持这两种播放器API,而且在将来可能还需要支
持新的媒体播放器,请问如何设计该应用程序?
绘制类图并编程模拟实现。
3.使用组合模式设计一个杀毒软件(AntiVirus)的框架,该软件既可以对某个文件夹
(Folder)杀毒,也可以对某个指定的文件(File)进行杀毒,文件种类包括文本文件TextFile、
图片文件ImageFile、视频文件VideoFile。
4.某教育机构组织结构如下图所示:
在该教育机构的OA系统中可以给各级办公室下发公文,试采用组合模式设计该机构的组织结构,绘制相应的类图并编程模拟实现,在客户端代码中模拟下发公文。
5.某软件公司为新开发的智能手机控制与管理软件提供了一键备份功能,通过该功能可以将原本存储在手机中的通信录、短信、照片、歌曲等资料一次性全部拷贝到移动存储介质(例如MMC卡或SD卡)中。
在实现过程中需要与多个已有的类进行交互,例如通讯录管理类、短信管理类等,为了降低系统的耦合度,试使用外观模式来设计并编程模拟实现该一键备份功能。
6.某信息系统需要提供一个数据处理和报表显示模块,该模块可以读取不同类型的文件中的数据并将数据转换成XML格式,然后对数据进行统计分析,最后以报表方式来显示数据。
由于该过程需要涉及到多个类,试使用外观模式设计该数据处理和报表显示模块。
虑到有些文件本身已经是XML格式,无须进行格式转换,为了让系统具有更好的扩展性,在系统设计中可以引入抽象外观类。
三、实验要求
1.结合实例,正确无误地绘制适配器模式、组合模式和外观模式的模式结构图;
2.使用任意一种面向对象编程语言实现适配器模式、组合模式和外观模式实例,代码运行正确无误。
四、实验步骤
1.结合实例,使用PowerDesigner绘制适配器模式实例结构图并用面向对象编程语言实现该模式实例;
2.结合实例,使用PowerDesigner绘制适配器模式实例结构图并用面向对象编程语言实现该模式实例;
3.结合实例,使用PowerDesigner绘制组合模式实例结构图并用面向对象编程语言实现该模式实例;
4.结合实例,使用PowerDesigner绘制组合模式实例结构图并用面向对象编程语言实现该模式实例;
5.结合实例,使用PowerDesigner绘制外观模式实例结构图并用面向对象编程语言实现该模式实例;
6.结合实例,使用PowerDesigner绘制外观模式实例结构图并用面向对象编程语言实现该模式实例。
五、实验结果
1.类图:
实现代码:
DataOperation.java
publicinterfaceDataOperation{
publicvoidsort(int[]n);
publicintsearch(int[]m,intn);
}
Adapter.java
publicclassAdapterimplementsDataOperation{privateBinarySearchbs;
privateQuickSortqs;
publicAdapter(){
qs=newQuicksort。
;
bs=newBinarySearch();
publicvoidsort(int[]n){
qs.quickSort(n);
publicintsearch(int[]m,intn){
returnbs.binarySearch(m,n);
QuickSort.java
publicclassQuicksort{
publicvoidquickSort(int[]n){
intsize=n.length;
quickSortMethod(n,0,size-1);
publicvoidquickSortMethod(intn[],intleft,intright){intq;
if(left<
right){
q=partition(n,left,right);
quickSortMethod(n,left,q-1);
quickSortMethod(n,q+1,right);
publicintpartition(intn[],intleft,intright){
intshaft=n[left];
inttemp=0;
while(left<
while(n[right]>
shaft)right--;
while(n[left]<
shaft)
left++;
if(n[left]==n[right]){right--;
}else{
temp=n[left];
n[left]=n[right];
n[right]=temp;
n[left]=shaft;
returnleft;
BinarySearch.java
publicclassBinarySearch{
publicintbinarySearch(intm[],intn){
Quicksortqs=newQuicksort。
intleft=0;
qs(m);
intright=m.length;
while(left<
=right){
intmiddle=(left+right)/2;
if(m[middle]==n)
returnmiddle;
elseif(m[middle]>
n)
right=middle-1;
else
left=middle+1;
return-1;
XMLUtil.java
importjavax.xml.parsers.*;
importorg.w3c.dom.*;
importorg.xml.sax.SAXException;
importjava.io.*;
publicclassXMLUtil{
//该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象
publicstaticObjectgetBean(){
try{
〃创建DOM文档对象
DocumentBuilderFactorydFactory=DocumentBuilderFactory.newlnstance();
DocumentBuilderbuilder=dFactory.newDocumentBuilder();
Documentdoc;
doc=builder.parse(newFile("
src/config.xml"
));
〃获取包含类名的文本节点
NodeListnl=doc.getElementsByTagName("
className"
);
NodeclassNode=nl.item(O).getFirstChild();
StringcName=classNode.getNodeValue();
//通过类名生成实例对象并将其返回
Classc=Class.forName(cName);
Objectobj=c.newlnstance();
returnobj;
}catch(Exceptione){
e.printStackTrace();
returnnuII;
Client.java
publicclassClient{
publicstaticvoidmain(Stringargs[]){
int[]m={5,4,8,7,9,2,3,1,0,6};
DataOperationdataoperation;
dataoperation=(DataOperation)XMLUtil.getBean();
dataoperation.sort(m);
for(inti=0;
i<
m.length;
i++){
System.out.print(m[i]+"
"
System.out.println("
\n"
+dataoperation.search(m,6));
2.类图:
Client
aPlayer
rzzr牙
□
+pJayfStringfileName).void
AA
WMPAdapter
-■的np.WindowsMediaPlayer
+play(StringfileName).void
WirtdowsMediaPlayer
+play(StringfileName)void
Player.java
publicinterfacePlayer{
publicabstractvoidplay(StringfileName);
WMPAdapter.java
publicclassWMPAdapterimplementsPlayer{privateWindowsMediaPlayerwmp;
publicWMPAdapter(){
wmp=newWindowsMediaPlayer();
publicvoidplay(StringfileName){
wmp.play(fileName);
RPAdapter.java
publicclassRPAdapterimplementsPlayer{
privateRealPlayerrp;
publicRPAdapter(){
rp=newRealPlayer。
rp.play(fileName);
WindowsMediaPlayer.java
publicclassWindowsMediaPlayer{
System.out.println(fileName+"
用WindowsMediaPlayer播放"
RealPlayer.java
publicclassRealPlayer{
用RealPlayer播放"
};
catch(Exceptione){
publicstaticvoidmain(String[]args){
Playerp;
p=(Player)XMLUtil.getBean();
p.play("
RushHour"
3.类图:
Afcslmctf-ih
[atwlracij
+kNVirus“void
AbstractFile.java
publicabstractclassAbstractFile{
publicabstractvoidkillVirus();
Folder.java
importjava.util.ArrayList;
publicclassFolderextendsAbstractFile{
privateStringfileName;
privateArrayList<
AbstractFile>
fileList=newArrayList<
();
publicvoidkillVirus(){
System.out.println(”对文件夹”+fileName+"
杀毒"
//调用文件夹内成员的杀毒方法
for(Objectobj:
fileList)
((AbstractFile)obj).killVirus();
publicvoidadd(AbstractFileabFile){
fileList.add(abFile);
publicvoidremove(AbstractFileabFile){
fileList.remove(abFile);
publicFolder(StringfileName){
this.fileName=fileName;
publicAbstractFilegetChild(intn){
returnfileList.get(n);
ImageFile.java
publicclassImageFileextendsAbstractFile{privateStringfileName;
System.out.println(”对图像文件”+fileName+"
publicImageFile(StringfileName){
TextFile.java
publicclassTextFileextendsAbstractFile{privateStringfileName;
System.out.println(”对文本文件”+fileName+"
publicTextFile(StringfileName){
publicclassVideoFileextendsAbstractFile{
System.out.println(”对视频文件”+fileName+"
publicVideoFile(StringfileName){
AbstractFilefile1,file2,file3;
Folderfolder1,folder2;
file1=newImageFile(”风景"
file2=newTextFile("
乔布斯全传"
file3=newVideoFile("
尖峰时刻”);
folder1=newFolder("
总文件夹"
folder2=newFolder("
子文件夹"
folder2.add(file1);
folder2.add(file2);
folder1.add(file3);
folder1.add(folder2);
folder1.killVirus();
4.
类图:
AbstractBranch.java
publicabstractclassAbstractBranch{
publicabstractvoidreceiveDoc();
publicabstractStringgetName();
AdOffice.java
publicclassAdOfficeextendsAbstractBranch{
privateStringname;
publicvoidreceiveDoc(){
System.out.println("
行政办公室收到文件"
publicAdOffice(Stringname){
this.name=name;
publicStringgetName(){
returnname;
EdOffice.java
publicclassEdOfficeextendsAbstractBranch{privateStringname;
教务办公室收到文件"
}
publicEdOffice(Stringname){
Branch.java
publicclassBranchextendsAbstractBranch{
AbstractBranch>
list;
publicBranch(Stringname){
System.out.println(name+"
分部收到文件”);
publicAbstractBranchgetChild(intn){
returnlist.get(n);
publicvoidadd(AbstractBranchabBranch){list.add(abBranch);
publicvoidremove(AbstractBranchabBranch){
list.remove(abBranch);
publicvoidsendDoc(AbstractBranchabBranch){
分部发送文件到"
+abBranch.getName());
abBranch.receiveDoc();
AbstractBranchab1,ab2;
Branchb1,b2,b3,b4;
ab1=newAdOffice(”行政办公室"
ab2=newEdOffice(”教务办公室”);
b1=newBranch("
北京"
b2=newBranch("
湖南"
b3=newBranch("
长沙"
b4=newBranch("
湘潭"
b1.sendDoc(b2);
b2.sendDoc(b3);
b2.sendDoc(b4);
b4.sendDoc(abl);
b3.sendDoc(ab2);
5.类图:
B0ckUpFac0<
fe