命令模式(command pattern) 撤销(undo) 详细解释

本文地址: http://blog.csdn.net/caroline_wendy

參考命令模式: http://blog.csdn.net/caroline_wendy/article/details/31379977

命令模式能够用于运行撤销(undo)操作.

详细方法:

1. 对象类中须要保存状态, 如level.

package command;public class CeilingFan {String location = "";int level;public static final int HIGH = 3;public static final int MEDIUM = 2;public static final int LOW = 1;public static final int OFF = 0;public CeilingFan(String location) {this.location = location;}public void high() {// turns the ceiling fan on to highlevel = HIGH;System.out.println(location + " ceiling fan is on high");} public void medium() {// turns the ceiling fan on to mediumlevel = MEDIUM;System.out.println(location + " ceiling fan is on medium");}public void low() {// turns the ceiling fan on to lowlevel = LOW;System.out.println(location + " ceiling fan is on low");}public void off() {// turns the ceiling fan offlevel = OFF;System.out.println(location + " ceiling fan is off");}public int getSpeed() {return level;}
}

2. 命令接口, 包括撤销(undo)操作, 即依据传入对象的状态參数, 推断详细的撤销动作.

/*** @time 2014年6月9日*/
package command;/*** @author C.L.Wang**/
public interface Command {public void execute();public void undo(); //撤销
}

3. 详细命令(Concrete Command)类须要实现, 撤销操作, 依据不同状态, 运行不同的撤销操作.

/*** @time 2014年6月16日*/
package command;/*** @author C.L.Wang**/
public class CeilingFanHighCommand implements Command {CeilingFan ceilingFan;int prevSpeed;public CeilingFanHighCommand(CeilingFan ceilingFan) {this.ceilingFan = ceilingFan;}/* (non-Javadoc)* @see command.Command#execute()*/@Overridepublic void execute() {// TODO Auto-generated method stubprevSpeed = ceilingFan.getSpeed();ceilingFan.high();}/* (non-Javadoc)* @see command.Command#undo()*/@Overridepublic void undo() {// TODO Auto-generated method stubif (prevSpeed == CeilingFan.HIGH) {ceilingFan.high();} else if (prevSpeed == CeilingFan.MEDIUM) {ceilingFan.medium();} else if (prevSpeed == CeilingFan.LOW) {ceilingFan.low();} else if (prevSpeed == CeilingFan.OFF) {ceilingFan.off();}}}package command;public class CeilingFanMediumCommand implements Command {CeilingFan ceilingFan;int prevSpeed;public CeilingFanMediumCommand(CeilingFan ceilingFan) {this.ceilingFan = ceilingFan;}public void execute() {prevSpeed = ceilingFan.getSpeed();ceilingFan.medium();}public void undo() {if (prevSpeed == CeilingFan.HIGH) {ceilingFan.high();} else if (prevSpeed == CeilingFan.MEDIUM) {ceilingFan.medium();} else if (prevSpeed == CeilingFan.LOW) {ceilingFan.low();} else if (prevSpeed == CeilingFan.OFF) {ceilingFan.off();}}
}package command;public class CeilingFanLowCommand implements Command {CeilingFan ceilingFan;int prevSpeed;public CeilingFanLowCommand(CeilingFan ceilingFan) {this.ceilingFan = ceilingFan;}public void execute() {prevSpeed = ceilingFan.getSpeed();ceilingFan.low();}public void undo() {if (prevSpeed == CeilingFan.HIGH) {ceilingFan.high();} else if (prevSpeed == CeilingFan.MEDIUM) {ceilingFan.medium();} else if (prevSpeed == CeilingFan.LOW) {ceilingFan.low();} else if (prevSpeed == CeilingFan.OFF) {ceilingFan.off();}}
}package command;public class CeilingFanOffCommand implements Command {CeilingFan ceilingFan;int prevSpeed;public CeilingFanOffCommand(CeilingFan ceilingFan) {this.ceilingFan = ceilingFan;}public void execute() {prevSpeed = ceilingFan.getSpeed();ceilingFan.off();}public void undo() {if (prevSpeed == CeilingFan.HIGH) {ceilingFan.high();} else if (prevSpeed == CeilingFan.MEDIUM) {ceilingFan.medium();} else if (prevSpeed == CeilingFan.LOW) {ceilingFan.low();} else if (prevSpeed == CeilingFan.OFF) {ceilingFan.off();}}
}

4. 接受者(Receiver)类实现撤销(undo)操作, 即在调用命令时, 保留命令, 运行撤销(undo)操作时, 调用保留的命令.

/*** @time 2014年6月16日*/
package command;/*** @author C.L.Wang**/
public class RemoteControl {Command[] onCommands; //开Command[] offCommands; //关Command undoCommand; //撤销public RemoteControl() {onCommands = new Command[7];offCommands = new Command[7];Command noCommand = new NoCommand();for (int i=0; i<7; ++i) { //初始化onCommands[i] = noCommand;offCommands[i] = noCommand;}undoCommand = noCommand;}public void setCommand (int slot, Command onCommand, Command offCommand) {this.onCommands[slot] = onCommand;this.offCommands[slot] = offCommand;}public void onButtonWasPushed(int slot) { //开启buttononCommands[slot].execute();undoCommand = onCommands[slot];}public void offButtonWasPushed(int slot) { //关闭buttonoffCommands[slot].execute();undoCommand = offCommands[slot];}public void undoButtonWasPushed() {undoCommand.undo();}public String toString() {StringBuffer stringBuffer = new StringBuffer();stringBuffer.append("\n------ Remote Control ------\n");for (int i=0; i<onCommands.length; ++i) {stringBuffer.append("[slot " + i + "] " + onCommands[i].getClass().getName()+ "    " + offCommands[i].getClass().getName() + "\n");}return stringBuffer.toString();}
}

5. 測试类, 调用不同的命令, 保存不同的状态, 运行撤销操作.

/*** @time 2014年6月16日*/
package command;import javax.crypto.spec.IvParameterSpec;/*** @author C.L.Wang**/
public class RemoteLoader {/*** @param args*/public static void main(String[] args) {// TODO Auto-generated method stubRemoteControl remoteControl = new RemoteControl();Light livingRoomLight = new Light("Living Room");Light kitchenLight = new Light("Kitchen");CeilingFan ceilingFan = new CeilingFan("Living Room");GarageDoor garageDoor = new GarageDoor("");Stereo stereo = new Stereo("Living Room");LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight);LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight);LightOnCommand kitchenLightOn = new LightOnCommand(kitchenLight);LightOffCommand kitchenLightOff = new LightOffCommand(kitchenLight);CeilingFanHighCommand ceilingFanHigh = new CeilingFanHighCommand(ceilingFan);CeilingFanMediumCommand ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan);CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan);GarageDoorOnCommand garageDoorOn = new GarageDoorOnCommand(garageDoor);GarageDoorOffCommand garageDoorOff = new GarageDoorOffCommand(garageDoor);StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo);StereoOffCommand stereoOffCommand = new StereoOffCommand(stereo);remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff); //设这遥控器remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);remoteControl.setCommand(2, ceilingFanHigh, ceilingFanOff);remoteControl.setCommand(3, ceilingFanMedium, ceilingFanOff);remoteControl.setCommand(4, stereoOnWithCD, stereoOffCommand);remoteControl.onButtonWasPushed(2); //快速remoteControl.offButtonWasPushed(2); //关闭快速System.out.println(remoteControl);remoteControl.undoButtonWasPushed(); //退回快速System.out.println();remoteControl.onButtonWasPushed(3); //中速System.out.println(remoteControl);remoteControl.undoButtonWasPushed(); //快速}}

6. 输出:

Living Room ceiling fan is on high
Living Room ceiling fan is off------ Remote Control ------
[slot 0] command.LightOnCommand    command.LightOffCommand
[slot 1] command.LightOnCommand    command.LightOffCommand
[slot 2] command.CeilingFanHighCommand    command.CeilingFanOffCommand
[slot 3] command.CeilingFanMediumCommand    command.CeilingFanOffCommand
[slot 4] command.StereoOnWithCDCommand    command.StereoOffCommand
[slot 5] command.NoCommand    command.NoCommand
[slot 6] command.NoCommand    command.NoCommandLiving Room ceiling fan is on highLiving Room ceiling fan is on medium------ Remote Control ------
[slot 0] command.LightOnCommand    command.LightOffCommand
[slot 1] command.LightOnCommand    command.LightOffCommand
[slot 2] command.CeilingFanHighCommand    command.CeilingFanOffCommand
[slot 3] command.CeilingFanMediumCommand    command.CeilingFanOffCommand
[slot 4] command.StereoOnWithCDCommand    command.StereoOffCommand
[slot 5] command.NoCommand    command.NoCommand
[slot 6] command.NoCommand    command.NoCommandLiving Room ceiling fan is on high

其余代码下载: http://download.csdn.net/detail/u012515223/7507147

设计模式 - 命令模式(command pattern) 撤销(undo) 具体解释相关推荐

  1. 设计模式:命令模式(Command Pattern)

    命令模式(Command Pattern): 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接受者是谁,也不知道请求的操作是哪个. 我们只需在程序运行时指定具体的请求接受者即可,此时 ...

  2. 解读设计模式----命令模式(Command Pattern)

    ***本文与作者原文有一定的偏差,其中加入了一部分是个人看法,详细请查看作者原文.*** 原文连接http://www.dofactory.com/Patterns/PatternCommand.as ...

  3. 乐在其中设计模式(C#) - 命令模式(Command Pattern)

    原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页] [源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...

  4. 设计模式系列3-----C++实现命令模式(Command Pattern)

    什么是命令模式? GoF的书的定义为:"Command pattern encapsulate request as an object, thereby letting you param ...

  5. 32命令模式(Command Pattern)

    耦合与变化:     耦合是软件不能抵御变化灾难的根本性原因.不仅实体对象与实体对象之间存在耦合关系,实体对象与行为操作之间也存在耦合关系.                               ...

  6. Java设计模式-命令模式Command

    定义 将来自客户端的请求传入一个对象,从而使你可用不同的请求对客户进行参数化.用于"行为请求者"与"行为实现者"解耦,可实现二者之间的松耦合,以便适应变化.分离 ...

  7. C++设计模式--命令模式(Command)

    概述 命令模式的结构很简单,但是对于消除代码间的耦合却有着重要的影响.命令模式就是一个函数对象:一个作为对象的函数.通过将函数封装为对象,就能够以参数的形式将其传递给其他函数或者对象,告诉它们在旅行请 ...

  8. 趣谈设计模式 | 命令模式(Command):将命令封装为对象

    文章目录 案例:智能遥控 命令模式 应用场景 队列请求 日志系统 总结 完整代码与文档 命令模式的应用场景较少,且不易理解,因此我也不好举例,所以下面的描述可能会存在一些问题,请见谅 案例:智能遥控 ...

  9. 设计模式-命令模式(Command)

    关注公众号 JavaStorm 获取更多成长. 大约需要6分钟读完.建议收藏后阅读. 命令模式把一个请求或者操作封装到一个对象中.命令模式允许系统使用不同的请求把客户端参数化,对请求排队或者记录请求日 ...

最新文章

  1. Docker4Dev #7 新瓶装老酒 – 使用 Windows Container运行ASP.NET MVC 2 + SQLExpress 应用
  2. const变量的使用方法。。
  3. 查看linux上redis的运行状态,Redis教程(七)使用info查看服务状态
  4. 【操作系统】信号量解决经典同步问题
  5. 使用echarts(二)自定义图表折线图
  6. SecureCRT日志上添加时间戳
  7. 静态导入 java面试_Java面试系列【静态导入】-静态导入,基础篇
  8. php实现单,双向链表,环形链表解决约瑟夫问题
  9. datatable的查询介绍
  10. Java 实现插入排序算法
  11. 免费版的进销存管理软件可以用吗
  12. mysql数据库工资管理系统_企业工资管理系统--数据库课程设计.doc
  13. 平面设计师okr_设计师如何定制OKR?
  14. python彩虹蛇_贪吃蛇还能这么玩?绝对是你从未体验过的全新版本(上)
  15. 3-23 实对称矩阵知识补充
  16. mathtype 7.4安装教程
  17. CSR Audio Sink Application User Guide
  18. 我所认识的Thayer博士
  19. 【Hgame2022】第一周misc和web题解
  20. 小猫爪:汽车电子小知识01- ISO15765(UDS on CAN)详解

热门文章

  1. Web负载均衡学习笔记之四层和七层负载均衡的区别
  2. “只有DBA才能导入由其他DBA导出的文件”各种解决办法
  3. Linux安装ImageMagick与JMagick完成过程及配置
  4. php 垃圾回收机制----写时复制和引用计数
  5. sqlsever2008数据库的备份与还原
  6. php的autoload机制
  7. C#关于伪静态页面的两种实现方法
  8. Exception Error log
  9. 深入理解Windows消息循环
  10. 二维傅里叶变换是怎么进行的?