命令模式的运用.doc_第1页
命令模式的运用.doc_第2页
命令模式的运用.doc_第3页
命令模式的运用.doc_第4页
命令模式的运用.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验四 命令模式的运用一、实验目的:命令模式将“请求”封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象,命令模式也支持可撤销的操作。在熟悉命令模式相关理论知识的基础上,使用命令模式实现图片处理程序。二、实验要求:使用命令模式实现图片处理程序,要求如下:1. 图片处理程序要有3张图片。2. 每张图片至少有3种操作。3. 实现类似遥控器的菜单,动态的选择对图片进行的处理。4. 要有“撤消操作”,撤消操作要可以撤销至最后一步。1、 设计并绘制该程序的类图;2、 依照设计的类图使用Java语言编写代码,并实现该程序;3、 除了核心的模式相关类实现外,提供测试环境,按照难度高低,分别是:a) 控制台程序,Client硬编码初始化模式和测试环境,运行结果文本输出;b) 控制台程序,Client初始化测试环境,并根据用户输入运算,运行结果文本输出;c) 设计并实现用户UI,Client初始化测试环境,并根据用户在UI控件上的输入运算,运行结果文本输出;三、实验内容:类图代码package Picture;public interface Command public void execute();public void undo();package Picture;public class NoCommand implements Command public void execute() public void undo() package Picture;public class Picture public static final int enlarge=1; /放大图片; public static final int rotating=2;/旋转图片; public static final int ensmall=3;/ 缩小图片; public static final int OFF= 0;/无操作; int way; String location;public Picture(String location) this.location = location;way=OFF; public void enlarge() way = enlarge;System.out.println(location + 已放大); public void rotating() way= rotating;System.out.println(location + 已旋转180);public void ensmall() way = ensmall;System.out.println(location + 已缩小); public void OFF() way= OFF;System.out.println(location + 无操作);public int getWay() return way;package Picture;public class PictureenlargeCommand implements Command Picture p;int prevWay; public PictureenlargeCommand(Picture p) this.p=p; public void execute() prevWay = p.getWay();p.enlarge(); public void undo() if (prevWay = p.enlarge) p.enlarge(); else if (prevWay =p.ensmall) p.ensmall(); else if (prevWay = p.rotating) p.rotating(); else if (prevWay = p.OFF) p.OFF();package Picture;public class PictureensmallCommand implements Command Picture p;int prevWay; public PictureensmallCommand(Picture p) this.p=p; public void execute() prevWay = p.getWay();p.ensmall(); public void undo() if (prevWay = p.enlarge) p.enlarge(); else if (prevWay =p.ensmall) p.ensmall(); else if (prevWay = p.rotating) p.rotating(); else if (prevWay = p.OFF) p.OFF();package Picture;public class PictureroatingCommand implements Command Picture p;int prevWay; public PictureroatingCommand(Picture p) this.p=p; public void execute() prevWay = p.getWay();p.rotating(); public void undo() if (prevWay = p.enlarge) p.enlarge(); else if (prevWay =p.ensmall) p.ensmall(); else if (prevWay = p.rotating) p.rotating(); else if (prevWay = p.OFF) p.OFF();package Picture;import java.util.LinkedList;import java.util.List;import java.util.*;/ This is the invoker/public class RemoteControlWithUndo Command enlargeCommands;Command ensmallCommands;Command rotatingCommands;List undoCommand;public RemoteControlWithUndo() enlargeCommands = new Command3;ensmallCommands = new Command3;rotatingCommands = new Command3;Command noCommand = new NoCommand();for(int i=0;i3;i+) enlargeCommandsi = noCommand;ensmallCommandsi = noCommand;rotatingCommandsi = noCommand;undoCommand=new LinkedList(); public void setCommand(int slot, Command enlargeCommand, Command ensmallCommand,Command rotatingCommand) enlargeCommandsslot = enlargeCommand;ensmallCommandsslot = ensmallCommand;rotatingCommandsslot = rotatingCommand; public void largeButtonWasPushed(int slot) enlargeCommandsslot.execute();undoCommand.add(enlargeCommandsslot); public void smallButtonWasPushed(int slot) ensmallCommandsslot.execute();undoCommand.add(ensmallCommandsslot);public void rotatingButtonWasPushed(int slot) rotatingCommandsslot.execute();undoCommand.add(rotatingCommandsslot); public void undoButtonWasPushed() undoCommand.remove(undoCommand.size()-1).undo();测试代码package Picture;public class RemoteLoader public static void main(String args) RemoteControlWithUndo remoteControl = new RemoteControlWithUndo();Picture p=new Picture3;PictureenlargeCommand enlarge=new PictureenlargeCommand3;PictureensmallCommand ensmall=new PictureensmallCommand3;PictureroatingCommand roating =new PictureroatingCommand3;for(int i=0;i3;i+)pi=new Picture(图+(i+1); enlargei = new PictureenlargeCommand(pi); ensmalli = new PictureensmallCommand(pi);roatingi = new PictureroatingCommand(pi);remoteControl.setCommand(i, enlargei, ensmalli, roatingi);System.out.println(图片处理);for(int i=0;i3;i+)remoteControl.largeButtonWasPushed(i);remoteControl.smallButtonWasPushe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论