设计模式实验五.docx

上传人:b****3 文档编号:4025987 上传时间:2023-05-06 格式:DOCX 页数:19 大小:273.39KB
下载 相关 举报
设计模式实验五.docx_第1页
第1页 / 共19页
设计模式实验五.docx_第2页
第2页 / 共19页
设计模式实验五.docx_第3页
第3页 / 共19页
设计模式实验五.docx_第4页
第4页 / 共19页
设计模式实验五.docx_第5页
第5页 / 共19页
设计模式实验五.docx_第6页
第6页 / 共19页
设计模式实验五.docx_第7页
第7页 / 共19页
设计模式实验五.docx_第8页
第8页 / 共19页
设计模式实验五.docx_第9页
第9页 / 共19页
设计模式实验五.docx_第10页
第10页 / 共19页
设计模式实验五.docx_第11页
第11页 / 共19页
设计模式实验五.docx_第12页
第12页 / 共19页
设计模式实验五.docx_第13页
第13页 / 共19页
设计模式实验五.docx_第14页
第14页 / 共19页
设计模式实验五.docx_第15页
第15页 / 共19页
设计模式实验五.docx_第16页
第16页 / 共19页
设计模式实验五.docx_第17页
第17页 / 共19页
设计模式实验五.docx_第18页
第18页 / 共19页
设计模式实验五.docx_第19页
第19页 / 共19页
亲,该文档总共19页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

设计模式实验五.docx

《设计模式实验五.docx》由会员分享,可在线阅读,更多相关《设计模式实验五.docx(19页珍藏版)》请在冰点文库上搜索。

设计模式实验五.docx

设计模式实验五

实验5结构型和行为型设计模式实验

实验学时:

2

每组人数:

1

实验类型:

3(1:

基础性2:

综合性3:

设计性4:

研究性)

实验要求:

1(1:

必修2:

选修3:

其它)

实验类别:

3(1:

基础2:

专业基础3:

专业4:

其它)

一、实验目的

熟练使用PowerDesigner和任意一种面向对象编程语言实现几种常见的结构型和行为型设计模式,包括代理模式、职责链模式和命令模式,理解每一种设计模式的模式动机,掌握模式结构,学习如何使用代码实现这些模式。

二、实验内容

1.在某应用软件中需要记录业务方法的调用日志,在不修改现有业务类的基础上为每一个类提供一个日志记录代理类,在代理类中输出日志,例如在业务方法method()调用之前输出“方法method()被调用,调用时间为2014-11-510:

10:

10”,调用之后如果没有抛异常则输出“方法method()调用成功”,否则输出“方法method()调用失败”。

在代理类中调用真实业务类的业务方法,使用代理模式设计该日志记录模块的结构,绘制类图并编程模拟实现。

2.某软件公司承接了某信息咨询公司的收费商务信息查询系统的开发任务,该系统的基本需求如下:

(1)在进行商务信息查询之前用户需要通过身份验证,只有合法用户才能够使用该查询系统;

(2)在进行商务信息查询时系统需要记录查询日志,以便根据查询次数收取查询费用。

该软件公司开发人员已完成了商务信息查询模块的开发任务,现希望能够以一种松耦合的方式向原有系统增加身份验证和日志记录功能,客户端代码可以无区别地对待原始的商务信息查询模块和增加新功能之后的商务信息查询模块,而且可能在将来还要在该信息查询模块中增加一些新的功能。

试使用代理模式设计并编程模拟实现该收费商务信息查询系统。

【提示:

使用保护代理和智能引用代理】

3.某企业的SCM(SupplyChainManagement,供应链管理)系统中包含一个采购审批子系统。

该企业的采购审批是分级进行的,即根据采购金额的不同由不同层次的主管人员来审批,主任可以审批5万元以下(不包括5万元)的采购单,副董事长可以审批5万元至10万元(不包括10万元)的采购单,董事长可以审批10万元至50万元(不包括50万元)的采购单,50万元及以上的采购单就需要开董事会讨论决定。

如下图所示:

试使用职责链模式设计并实现该系统。

4.某公司欲开发一个软件系统的在线文档帮助系统,用户可以在任何一个查询上下文中输入查询关键字,如果当前查询环境下没有相关内容,则系统会将查询按照一定的顺序转发给其他查询环境。

基于上述需求,试采用职责链模式对该系统进行设计。

5.房间中的开关就是命令模式的一个实例,现用命令模式来模拟开关的功能,可控制对象包括电灯和电风扇,绘制相应的类图并编程模拟实现。

6.某软件公司欲开发一个基于Windows平台的公告板系统。

系统提供一个主菜单(Menu),在主菜单中包含了一些菜单项(MenuItem),可以通过Menu类的addMenuItem()方法增加菜单项。

菜单项的主要方法是click(),每一个菜单项包含一个抽象命令类,具体命令类包括OpenCommand(打开命令),CreateCommand(新建命令),EditCommand(编辑命令)等,命令类具有一个execute()方法,用于调用公告板系统界面类(BoardScreen)的open()、create()、edit()等方法。

现使用命令模式设计该系统,使得MenuItem类与BoardScreen类的耦合度降低,绘制类图并编程实现。

三、实验要求

1.结合实例,正确无误地绘制代理模式、职责链模式和命令模式的模式结构图;

2.使用任意一种面向对象编程语言实现代理模式、职责链模式和命令模式实例,代码运行正确无误。

四、实验步骤

1.结合实例,使用PowerDesigner绘制代理模式实例结构图并用面向对象编程语言实现该模式实例;

2.结合实例,使用PowerDesigner绘制代理模式实例结构图并用面向对象编程语言实现该模式实例;

3.结合实例,使用PowerDesigner绘制职责链模式实例结构图并用面向对象编程语言实现该模式实例;

4.结合实例,使用PowerDesigner绘制职责链模式实例结构图并用面向对象编程语言实现该模式实例;

5.结合实例,使用PowerDesigner绘制命令模式实例结构图并用面向对象编程语言实现该模式实例;

6.结合实例,使用PowerDesigner绘制命令模式实例结构图并用面向对象编程语言实现该模式实例。

五、实验结果

1.类图:

实现代码:

publicinterfaceAbstractLog{

publicvoidmethod();

}

importclassLogProxyimplementsAbstractLog{

privateLogRecordlogRecord=newLogRecord();

privateSimpleDateFormatSDF=newSimpleDateFormat("yyyy-MM-ddHH:

mm:

ss");

privateStringtime=(newDate());

publicvoidmethod(){

"方法Method被调用,调用时间为:

"+time);

try{

();

"方法method()调用成功");

}catch(Exceptione){

"方法method()调用失败");

}

}

}

publicclassLogRecordimplementsAbstractLog{

publicvoidmethod(){

"真实业务方法");

}

}

import.*;

publicclassXMLUtil

{

etFirstChild();

StringcName=();

ava

publicinterfaceAbstractSearch{

Stringsearch(Stringuid);

}

publicclassSearchProxyimplementsAbstractSearch{

privateRealSearchrealSearch=newRealSearch();

privateValidatorvalidator;

publicStringsearch(Stringuid){

if(Validate(uid)){

Stringresult=(uid);

Log(uid);

returnresult;

}

else

returnnull;

}

privatevoidLog(Stringuid){

"记录日志");

}

privatebooleanValidate(Stringuid){

validator=newValidator();

return(uid);

}

}

publicclassRealSearchimplementsAbstractSearch{

publicStringsearch(Stringuid){

+"在进行具体查询");

return"具体查询内容";

}

}

publicclassValidator{

publicbooleanvalidate(Stringuid){

if(uid=="uid"){

"验证通过");

returntrue;

}

"验证失败");

returnfalse;

}

}

publicclassClient{

publicstaticvoidmain(String[]args){

AbstractSearchas;

as=(AbstractSearch)();

("uid");

}

}

类图:

实现代码:

publicabstractclassLeader{

protectedLeadersuccessor;

protectedStringname;

publicLeader(){

}

publicabstractvoidhandleRequest(PurchaseRequestpRequest);

publicLeader(Stringname){

=name;

}

publicvoidsetSuccessor(Leadersuccessor){

=successor;

}

}

publicclassDirectorextendsLeader{

publicDirector(Stringname){

super(name);

}

publicvoidhandleRequest(PurchaseRequestpRequest){

if()<50000)

"主任"++"审批采购单,采购金额为"+());

elseif!

=null)

}

}

publicclassManagerextendsLeader{

publicManager(Stringname){

super(name);

}

publicvoidhandleRequest(PurchaseRequestpRequest){

if()<100000)

"副董事长"++"审批采购单,采购金额为"+());

elseif!

=null)

}

}

publicclassGeneralManagerextendsLeader{

publicGeneralManager(Stringname){

super(name);

}

publicvoidhandleRequest(PurchaseRequestpRequest){

if()<500000)

"董事长"++"审批采购单,采购金额为"+());

elseif!

=null)

}

}

publicclassBoardOfDirectorsextendsLeader{

publicBoardOfDirectors(){

}

publicvoidhandleRequest(PurchaseRequestpRequest){

"董事会讨论决定是否批准采购单,采购金额为"+());

}

}

publicclassPurchaseRequest{

privatedoublemoney;

publicPurchaseRequest(doublemoney){

=money;

}

publicdoublegetMoney(){

returnmoney;

}

publicvoidsetMoney(doublenewMoney){

money=newMoney;

}

}

publicclassClient{

publicstaticvoidmain(String[]args){

Leaderl1,l2,l3,l4;

PurchaseRequestpr1,pr2,pr3,pr4,pr5;

l1=newDirector("张三");

l2=newManager("李四");

l3=newGeneralManager("王五");

l4=newBoardOfDirectors();

pr1=newPurchaseRequest(20000);

pr2=newPurchaseRequest(60000);

pr3=newPurchaseRequest(120000);

pr4=newPurchaseRequest(520000);

(l2);

(l3);

(l4);

(pr1);

(pr2);

(pr3);

(pr4);

}

}

类图:

实现代码:

publicabstractclassSearchContext{

protectedSearchContextsuccessor;

publicvoidsetSuccessor(SearchContextsuccessor){

=successor;

}

publicabstractvoidsearch(Stringkey);

}

publicclassSearchContext1extendsSearchContext{

publicvoidsearch(Stringkey){

if(key=="key1")

"在Context1中查到相关内容");

elseif!

=null)

}

}

publicclassSearchContext2extendsSearchContext{

publicvoidsearch(Stringkey){

if(key=="key2")

"在Context2中查到相关内容");

elseif!

=null)

else

"未查到相关内容");

}

}

publicclassClient{

publicstaticvoidmain(String[]args){

SearchContextsc1,sc2;

sc1=newSearchContext1();

sc2=newSearchContext2();

(sc2);

("key2");

}

}

类图:

实现代码:

publicclassSwitch{

privateAbstractCommandabstractCommand;

publicvoidsetComman(AbstractCommandabstractCommand){

=abstractCommand;

}

publicvoidopen(){

();

}

publicvoidclose(){

();

}

}

publicabstractclassAbstractCommand{

publicabstractvoidopen();

publicabstractvoidclose();

}

publicclassFunCommandextendsAbstractCommand{

privateFunfun=newFun();

publicvoidopen(){

();

}

publicvoidclose(){

();

}

}

publicclassLightCommandextendsAbstractCommand{

privateLightlight=newLight();

publicvoidopen(){

();

}

publicvoidclose(){

();

}

}

publicclassFun{

publicvoidopen(){

"开风扇");

}

publicvoidclose(){

"关风扇");

}

}

Light.java

publicclassLight{

publicvoidopen(){

"开灯");

}

publicvoidclose(){

"关灯");

}

}

publicclassClient{

publicstaticvoidmain(String[]args){

Switchs=newSwitch();

AbstractCommandac;

ac=(AbstractCommand)();

(ac);

();

();

}

}

类图:

实现代码:

importclassMenu{

publicArrayListmenuItemList=newArrayList();

publicvoidaddMenuItem(MenuItemmenuItem){

(menuItem);

+"功能加入主菜单");

}

publicvoidremoveMenuItem(MenuItemmenuItem){

(menuItem);

+"功能从主菜单中移除");

}

}

publicclassMenuItem{

privateStringname;

publicAbstractCommandabstractCommand;

publicvoidclick(){

();

}

publicMenuItem(Stringname){

=name;

}

publicvoidsetCommand(AbstractCommandabstractCommand){

=abstractCommand;

}

publicStringgetName(){

returnname;

}

}

AbstractCommand.java

publicabstractclassAbstractCommand{

publicabstractvoidexecute();

}

publicclassOpenCommandextendsAbstractCommand{

publicBoardScreenboardScreen=newBoardScreen();

publicvoidexecute(){

();

}

}

publicclassCreateCommandextendsAbstractCommand{

publicBoardScreenboardScreen=newBoardScreen();

publicvoidexecute(){

();

}

}

publicclassEditCommandextendsAbstractCommand{

publicBoardScreenboardScreen=newBoardScreen();

publicvoidexecute(){

();

}

}

publicclassBoardScreen{

publicvoidopen(){

"打开");

}

publicvoidcreate(){

"新建");

}

publicvoidedit(){

"编辑");

}

}

publicclassClient{

publicstaticvoidmain(String[]args){

Menum=newMenu();

MenuItemm1,m2,m3;

AbstractCommandac1,ac2,ac3;

m1=newMenuItem("打开");

m2=newMenuItem("新建");

m3=newMenuItem("编辑");

ac1=newOpenCommand();

ac2=newCreateCommand();

ac3=newEditCommand();

(ac1);

(ac2);

(ac3);

();

();

();

(m1);

(m2);

(m3);

(m3);

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2