命令模式(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 high
level = HIGH;
System.out.println(location + " ceiling fan is on high"); } public void medium() {
// turns the ceiling fan on to medium
level = MEDIUM;
System.out.println(location + " ceiling fan is on medium");
} public void low() {
// turns the ceiling fan on to low
level = LOW;
System.out.println(location + " ceiling fan is on low");
} public void off() {
// turns the ceiling fan off
level = 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()
*/
@Override
public void execute() {
// TODO Auto-generated method stub
prevSpeed = ceilingFan.getSpeed();
ceilingFan.high();
} /* (non-Javadoc)
* @see command.Command#undo()
*/
@Override
public void undo() {
// TODO Auto-generated method stub
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 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) { //开启button
onCommands[slot].execute();
undoCommand = onCommands[slot];
} public void offButtonWasPushed(int slot) { //关闭button
offCommands[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 stub RemoteControl 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.NoCommand Living Room ceiling fan is on high Living 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.NoCommand Living Room ceiling fan is on high

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

设计模式 - 命令模式(command pattern) 撤销(undo) 具体解释的更多相关文章

  1. 设计模式 - 命令模式(command pattern) 宏命令(macro command) 具体解释

    命令模式(command pattern) 宏命令(macro command) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考: 命名模式(撤销) ...

  2. 设计模式 - 命令模式(command pattern) 具体解释

    命令模式(command pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 命令模式(command pattern) : 将请求封装成对 ...

  3. 设计模式 - 命令模式(command pattern) 多命令 具体解释

    命令模式(command pattern) 多命令 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.csdn.ne ...

  4. C#设计模式——命令模式(Command Pattern)

    一.概述通常来说,“行为请求者”与“行为实现者”是紧耦合的.但在某些场合,比如要对行为进行“记录.撤销/重做.事务”等处理,这种无法抵御变化的紧耦合是不合适的.在这些情况下,将“行为请求者”与“行为实 ...

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

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

  6. 二十四种设计模式:命令模式(Command Pattern)

    命令模式(Command Pattern) 介绍将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可取消的操作. 示例有一个Message实体类,某个 ...

  7. 设计模式-15命令模式(Command Pattern)

    1.模式动机 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需在程序运行时指定具体的请求接收者即可,此时,可以使用命令模式来进行设计,使 ...

  8. 设计模式(六):控制台中的“命令模式”(Command Pattern)

    今天的博客中就来系统的整理一下“命令模式”.说到命令模式,我就想起了控制台(Console)中的命令.无论是Windows操作系统(cmd.exe)还是Linux操作系统(命令行式shell(Comm ...

  9. 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 具体解释

    组合模式(composite pattern) 迭代器(iterator) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考组合模式(composit ...

随机推荐

  1. Java文件上传与下载

    文件上传与下载可谓上网中的常见现象.apache为我们准备了用于文件上传与下载的两个jar包(commons-fileupload-1.2.1.jar,commons-io-1.4.jar).我们在w ...

  2. Mysql自带profiling性能分析工具使用分享

    1. show variables like '%profiling%';(查看profiling信息)       2. set profiling=1;(开启profiling)   3. 执行S ...

  3. Spring 中Bean的装配方式

    最近又买了一本介绍SSM框架的书,是由黑马程序员编写的,书上讲的很好理解,边看边总结一下.主要总结一下bean的装配方式. Bean的装配可以理解为依赖系统注入,Bean的装配方式即Bean依赖注入的 ...

  4. 【Mac 10.13.0】安装 libimobiledevice,提示报错:warning: unable to access '/Users/lucky/.config/git/attributes': Permission denied解决方案

    打开终端,执行命令: 1.sudo chown -R XXX /usr/local  (XXX表示当前用户名) 2.ruby -e "$(curl -fsSL https://raw.git ...

  5. Linux内核编译安装

    1. .config 复制一份当前系统编译时的配置,在/usr/src目录下 $ ls /usr/src/ linux-headers-4.10.0-35 linux-headers-4.8.0-36 ...

  6. QString 与中文问题

    原文请看:http://www.cnblogs.com/phoenixlaozhu/articles/2553180.html (更新:本文的姊妹篇Qt5与中文问题) 首先呢,声明一下,QString ...

  7. 转载:tar命令批量解压方法总结

    由于linux的tar命令不支持批量解压,所以很多网友编写了好多支持批量解压的shell命令,收集了一下,供大家分享: 第一:for tar in *.tar.gz;  do tar xvf $tar ...

  8. python 学习笔记 - Queue & Pipes,进程间通讯

    上面写了Python如何创建多个进程,但是前面文章中创建的进程都是哑巴和聋子,自己顾自己执行,不会相互交流.那么如何让进程间相互说说话呢?Python为我们提供了一个函数multiprocessing ...

  9. Python协程(中)

    协程嵌套 使用async可以定义协程,协程用于耗时的io操作,我们也可以封装更多的io操作过程,这样就实现了嵌套的协程,即一个协程中await了另外一个协程,如此连接起来. import asynci ...

  10. 【BZOJ 3307】 3307: 雨天的尾巴 (线段树+树链剖分)

    3307: 雨天的尾巴 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 458  Solved: 210 Description N个点,形成一个树状结 ...