1、实验 8组合模式、代理模式实验学号:123012012137姓名:张超红实验目的:加深对组合模式、代理模式原理的理解实验环境:C#.Net/VC+.Net 或 MyEclipse(Java)等实验内容(一):使用组合模式设计一个杀毒软件框架,该软件既可以对一个文件夹杀毒,也可以对一个文件杀毒,文件种类包括文本文件、图片文件、视频文件。绘制类图并编程实现。实验过程:、构建实现场景,画出 UML 类图、实现代码,见演示源码public abstract class AntiVirus public void killVirus()import java.util.ArrayList;public
2、class Folder extends AntiVirus private ArrayList fileList= new ArrayList();public void folder(AntiVirus fileName)fileList.add(fileName);public void killVirus()for(Object object:fileList)System.out.println(object.getClass().getName();(AntiVirus)object).killVirus();public class ImageFile extends AntiV
3、irus public void killVirus()System.out.println(“ImageFile 杀毒中“);public class TextFile extends AntiVirus public void killVirus()System.out.println(“TextFile杀毒中“);public class VideoFile extends AntiVirus public void killVirus()System.out.println(“VideoFile 杀毒中“);public class Client /* param args*/publ
4、ic static void main(String args) AntiVirus file1,file2,file3,file4,file5,file6;Folder floder1,floder2,floder3;file1=new ImageFile();file2=new TextFile();file3=new VideoFile();floder1 =new Folder();floder1.folder(file1);floder1.folder(file2);floder1.folder(file3);file4=new ImageFile();file5=new TextF
5、ile();file6=new VideoFile();floder2=new Folder();floder2.folder(file1);floder2.folder(file2);floder2.folder(file3);floder3=new Folder();floder3.folder(floder1);floder3.folder(floder2);floder3.killVirus();实验内容(二):某信息咨询公司推出收费的在线商业信息查询模块,需要对查询用户进行身份验证,并记录查询日志,以便根据查询次数收取查询费用,现使用代理模式设计该系统。实验过程:构建实现场景,画出
6、UML 类图实现代码package agent;/* * author 张超红*/public class User public User() / TODO 自动生成的构造函数存根private String userName;private String password;/是否被允许private boolean isPermission;/查询次数Integer selectCount;/查询费用private double selectOffFees;public String getUserName() return userName;public void setUserName
7、(String userName) this.userName = userName;public String getPassword() return password;public void setPassword(String password) this.password = password;public boolean isPermission() return isPermission;public void setPermission(boolean isPermission) this.isPermission = isPermission;public Integer g
8、etSelectCount() return selectCount;public void setSelectCount(Integer selectCount) this.selectCount = selectCount;public double getSelectOffFees() return selectOffFees;public void setSelectOffFees(double selectOffFees) this.selectOffFees = selectOffFees;package agent;/* * author 张超红*/public class Pe
9、rmissionProxy implements AbstractCompony private User user;private RealCompony permission=new RealCompony();public PermissionProxy() / TODO 自动生成的构造函数存根/* (非 Javadoc)* see agent.AbstractCompony#selectInfor()*/Overridepublic void selectInfor() / TODO 自动生成的方法存根if(user.isPermission()if(user.getSelectCou
10、nt()0)permission.selectInfor();user.selectCount-;if(user.selectCount=0)user.setPermission(false);System.out.println(“您查询次数已经到达上限,请缴费.“ );elseSystem.out.println(“要在本系统中查询商业信息请先注册登录.“ );public User construct()return new User();package agent;/* * author 张超红*/public interface AbstractCompony /查询用户信息publ
11、ic void selectInfor();package agent;public class RealCompony implements AbstractCompony public RealCompony() / TODO 自动生成的构造函数存根/* (非 Javadoc)* see agent.AbstractCompony#selectInfor()*/Overridepublic void selectInfor() / TODO 自动生成的方法存根System.out.println(“用户正在查询商业信息.“ );package agent;/* * author 张超红*/public class Client public Client() / TODO 自动生成的构造函数存根public static void main(String args) RealCompony permission=new RealCompony();PermissionProxy per=new PermissionProxy();User user=new User();user.setSelectCount(2);user.setPermission(true);permission.selectInfor();实验讨论(效果分析):