您好,欢迎访问三七文档
云南大学软件学院实验报告序号:姓名:肖正文学号:20121120141专业:软件工程日期:2014/12/22成绩:实验四命令模式的运用一、实验目的:命令模式将“请求”封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象,命令模式也支持可撤销的操作。在熟悉命令模式相关理论知识的基础上,使用命令模式实现图片处理程序。二、实验要求:使用命令模式实现图片处理程序,要求如下:1.图片处理程序要有3张图片。2.每张图片至少有3种操作。3.实现类似遥控器的菜单,动态的选择对图片进行的处理。4.要有“撤消操作”,撤消操作要可以撤销至最后一步。1、设计并绘制该程序的类图;2、依照设计的类图使用Java语言编写代码,并实现该程序;3、除了核心的模式相关类实现外,提供测试环境,按照难度高低,分别是:a)控制台程序,Client硬编码初始化模式和测试环境,运行结果文本输出;b)控制台程序,Client初始化测试环境,并根据用户输入运算,运行结果文本输出;c)设计并实现用户UI,Client初始化测试环境,并根据用户在UI控件上的输入运算,运行结果文本输出;三、实验内容:1体系结构2类图1.命令2.packagexiao.it.Commend;3./**4.*命令接口5.*@authorAdministrator6.*7.*/8.publicinterfaceCommand{9.publicStringexecute();10.}每张图片1的控制命令1切割packagexiao.it.ConcreteCommend1;importxiao.it.Commend.Command;importxiao.it.Picture.Picture1;publicclassCutCommandimplementsCommand{Picture1p1=null;publicCutCommand(Picture1p){this.p1=p;}@OverridepublicStringexecute(){//TODOAuto-generatedmethodstubreturnp1.cut();}}2旋转packagexiao.it.ConcreteCommend1;importxiao.it.Commend.Command;importxiao.it.Picture.Picture1;/***对图像1进行旋转*@authorAdministrator**/publicclassRotaCommandimplementsCommand{Picture1p1=null;publicRotaCommand(Picture1p){this.p1=p;}@OverridepublicStringexecute(){//TODOAuto-generatedmethodstubreturnp1.rota();}}3平移packagexiao.it.ConcreteCommend1;importxiao.it.Commend.Command;importxiao.it.Picture.Picture1;publicclassTransCommandimplementsCommand{Picture1p1=null;publicTransCommand(Picture1p){this.p1=p;}@OverridepublicStringexecute(){//TODOAuto-generatedmethodstubreturnp1.trans();}}图片2的具体命令1放大packagexiao.it.ConcreteCommend2;importxiao.it.Commend.Command;importxiao.it.Picture.Picture1;importxiao.it.Picture.Picture2;/***对图片2进行放大*@authorAdministrator**/publicclassFangdaCommandimplementsCommand{Picture2p2=null;publicFangdaCommand(Picture2p){this.p2=p;}@OverridepublicStringexecute(){//TODOAuto-generatedmethodstubreturnp2.Fangda();}}2缩小packagexiao.it.ConcreteCommend2;importxiao.it.Commend.Command;importxiao.it.Picture.Picture2;/***对图片2进行缩小*@authorAdministrator**/publicclassSuoxiCommandimplementsCommand{Picture2p2=null;publicSuoxiCommand(Picture2p){this.p2=p;}@OverridepublicStringexecute(){//TODOAuto-generatedmethodstubreturnp2.Suoxi();}}3伪彩色packagexiao.it.ConcreteCommend2;importxiao.it.Commend.Command;importxiao.it.Picture.Picture2;publicclassWecaiseCommandimplementsCommand{Picture2p2=null;publicWecaiseCommand(Picture2p){this.p2=p;}@OverridepublicStringexecute(){//TODOAuto-generatedmethodstubreturnp2.Wecaise();}}图片3处理方式滤波packagexiao.it.ConcreteCommend3;importxiao.it.Commend.Command;importxiao.it.Picture.Picture2;importxiao.it.Picture.Picture3;/*8*对图片3进行滤波*/publicclassLuBoCommandimplementsCommand{Picture3p3=null;publicLuBoCommand(Picture3p){this.p3=p;}@OverridepublicStringexecute(){//TODOAuto-generatedmethodstubreturnp3.LuBo();}}2锐化packagexiao.it.ConcreteCommend3;importxiao.it.Commend.Command;importxiao.it.Picture.Picture3;/***对图片3进行锐化*@authorAdministrator**/publicclassRuihaCommandimplementsCommand{Picture3p3=null;publicRuihaCommand(Picture3p){this.p3=p;}@OverridepublicStringexecute(){//TODOAuto-generatedmethodstubreturnp3.Ruiha();}}曾强packagexiao.it.ConcreteCommend3;importxiao.it.Commend.Command;importxiao.it.Picture.Picture3;/***对图片3进行增强*@authorAdministrator**/publicclassZenQiangCommandimplementsCommand{Picture3p3=null;publicZenQiangCommand(Picture3p){this.p3=p;}@OverridepublicStringexecute(){//TODOAuto-generatedmethodstubreturnp3.ZenQiang();}}远程控制器packagexiao.it.control;importjava.util.ArrayList;importxiao.it.Commend.Command;importxiao.it.NullConcreteCommend.Nocommand;/***带有撤销的三种操作*@authorAdministrator**/publicclassRenoteControler{Command[]com1;Command[]com2;Command[]com3;publicstaticintlength;//用来存储一系列的撤销命令,以达到栈的目的ArrayListCommandundocommnd=newArrayListCommand();Commandnocommand=newNocommand();CommandcurrentCommand;//对命令初始化publicRenoteControler(){com1=newCommand[3];com2=newCommand[3];com3=newCommand[3];for(inti=0;i3;i++){com1[i]=nocommand;com2[i]=nocommand;com3[i]=nocommand;}}/***为每一张图片分配三种操作*@parampicture*@paramcom1*@paramcom2*@paramcom3*/publicvoidsetCommond(intpicture,Commandcom1,Commandcom2,Commandcom3){this.com1[picture]=com1;this.com2[picture]=com2;this.com3[picture]=com3;}/***对指定图片进行第一种操作*@parampicture*@return*/publicStringoperation1(intpicture){this.undocommnd.add(com1[picture]);returncom1[picture].execute();}/***对指定图片进行第二种操作*@parampicture*@return*/publicStringoperation2(intpicture){this.undocommnd.add(com2[picture]);returncom2[picture].execute();}/***对指定图片进行第三种操作*@parampicture*@return*/publicStringoperation3(intpicture){this.undocommnd.add(com3[picture]);returncom3[picture].execute();}/***对图片进行撤销操作*@return*/publicvoidUndo(){length=undocommnd.size();System.out.println(length);}publicStringupstep(){length--;if(length=0){returnundocommnd.get(length).execute();}else{returnnull;}}publicStringtoString(){return图片1\n{1旋转2切割3平移}\n\n\n图片2\n{1放大2缩小3伪彩色}\n\n\n图片3\n{1锐化2滤波3增强};}}无命令状态packagexiao.it.NullConcreteCommend;importxiao.it.Commend.Command;publicclassNocommandimplementsCommand{@OverridepublicStringexecute(){//TODOAuto-generatedmeth
本文标题:肖正文命令模式
链接地址:https://www.777doc.com/doc-4650736 .html