您好,欢迎访问三七文档
实验四命令模式的运用一、实验目的:命令模式将“请求”封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象,命令模式也支持可撤销的操作。在熟悉命令模式相关理论知识的基础上,使用命令模式实现图片处理程序。二、实验要求:使用命令模式实现图片处理程序,要求如下:1.图片处理程序要有3张图片。2.每张图片至少有3种操作。3.实现类似遥控器的菜单,动态的选择对图片进行的处理。4.要有“撤消操作”,撤消操作要可以撤销至最后一步。1、设计并绘制该程序的类图;2、依照设计的类图使用Java语言编写代码,并实现该程序;3、除了核心的模式相关类实现外,提供测试环境,按照难度高低,分别是:a)控制台程序,Client硬编码初始化模式和测试环境,运行结果文本输出;b)控制台程序,Client初始化测试环境,并根据用户输入运算,运行结果文本输出;c)设计并实现用户UI,Client初始化测试环境,并根据用户在UI控件上的输入运算,运行结果文本输出;三、实验内容:类图代码packagePicture;publicinterfaceCommand{publicvoidexecute();publicvoidundo();}packagePicture;publicclassNoCommandimplementsCommand{publicvoidexecute(){}publicvoidundo(){}}packagePicture;publicclassPicture{publicstaticfinalintenlarge=1;//放大图片;publicstaticfinalintrotating=2;//旋转图片;publicstaticfinalintensmall=3;//缩小图片;publicstaticfinalintOFF=0;//无操作;intway;Stringlocation;publicPicture(Stringlocation){this.location=location;way=OFF;}publicvoidenlarge(){way=enlarge;System.out.println(location+已放大);}publicvoidrotating(){way=rotating;System.out.println(location+已旋转180°);}publicvoidensmall(){way=ensmall;System.out.println(location+已缩小);}publicvoidOFF(){way=OFF;System.out.println(location+无操作);}publicintgetWay(){returnway;}}packagePicture;publicclassPictureenlargeCommandimplementsCommand{Picturep;intprevWay;publicPictureenlargeCommand(Picturep){this.p=p;}publicvoidexecute(){prevWay=p.getWay();p.enlarge();}publicvoidundo(){if(prevWay==p.enlarge){p.enlarge();}elseif(prevWay==p.ensmall){p.ensmall();;}elseif(prevWay==p.rotating){p.rotating();}elseif(prevWay==p.OFF){p.OFF();}}}packagePicture;publicclassPictureensmallCommandimplementsCommand{Picturep;intprevWay;publicPictureensmallCommand(Picturep){this.p=p;}publicvoidexecute(){prevWay=p.getWay();p.ensmall();}publicvoidundo(){if(prevWay==p.enlarge){p.enlarge();}elseif(prevWay==p.ensmall){p.ensmall();;}elseif(prevWay==p.rotating){p.rotating();}elseif(prevWay==p.OFF){p.OFF();}}}packagePicture;publicclassPictureroatingCommandimplementsCommand{Picturep;intprevWay;publicPictureroatingCommand(Picturep){this.p=p;}publicvoidexecute(){prevWay=p.getWay();p.rotating();}publicvoidundo(){if(prevWay==p.enlarge){p.enlarge();}elseif(prevWay==p.ensmall){p.ensmall();;}elseif(prevWay==p.rotating){p.rotating();}elseif(prevWay==p.OFF){p.OFF();}}}packagePicture;importjava.util.LinkedList;importjava.util.List;importjava.util.*;////Thisistheinvoker//publicclassRemoteControlWithUndo{Command[]enlargeCommands;Command[]ensmallCommands;Command[]rotatingCommands;ListCommandundoCommand;publicRemoteControlWithUndo(){enlargeCommands=newCommand[3];ensmallCommands=newCommand[3];rotatingCommands=newCommand[3];CommandnoCommand=newNoCommand();for(inti=0;i3;i++){enlargeCommands[i]=noCommand;ensmallCommands[i]=noCommand;rotatingCommands[i]=noCommand;}undoCommand=newLinkedListCommand();}publicvoidsetCommand(intslot,CommandenlargeCommand,CommandensmallCommand,CommandrotatingCommand){enlargeCommands[slot]=enlargeCommand;ensmallCommands[slot]=ensmallCommand;rotatingCommands[slot]=rotatingCommand;}publicvoidlargeButtonWasPushed(intslot){enlargeCommands[slot].execute();undoCommand.add(enlargeCommands[slot]);}publicvoidsmallButtonWasPushed(intslot){ensmallCommands[slot].execute();undoCommand.add(ensmallCommands[slot]);}publicvoidrotatingButtonWasPushed(intslot){rotatingCommands[slot].execute();undoCommand.add(rotatingCommands[slot]);}publicvoidundoButtonWasPushed(){undoCommand.remove(undoCommand.size()-1).undo();}}测试代码packagePicture;publicclassRemoteLoader{publicstaticvoidmain(String[]args){RemoteControlWithUndoremoteControl=newRemoteControlWithUndo();Picture[]p=newPicture[3];PictureenlargeCommand[]enlarge=newPictureenlargeCommand[3];PictureensmallCommand[]ensmall=newPictureensmallCommand[3];PictureroatingCommand[]roating=newPictureroatingCommand[3];for(inti=0;i3;i++){p[i]=newPicture(图+(i+1));enlarge[i]=newPictureenlargeCommand(p[i]);ensmall[i]=newPictureensmallCommand(p[i]);roating[i]=newPictureroatingCommand(p[i]);remoteControl.setCommand(i,enlarge[i],ensmall[i],roating[i]);}System.out.println(图片处理);for(inti=0;i3;i++){remoteControl.largeButtonWasPushed(i);remoteControl.smallButtonWasPushed(i);remoteControl.rotatingButtonWasPushed(i);}System.out.println(进行撤销操作);for(;remoteControl.undoCommand.size()!=0;)remoteControl.undoButtonWasPushed();}}运行结果四、实验总结:本次试验,学会了命令模式的简单应用,在熟悉命令模式相关理论知识的基础上,使用命令模式实现了图片处理程序。指导教师签名:
本文标题:命令模式的运用
链接地址:https://www.777doc.com/doc-4679217 .html