命令模式(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. Linux时间结构体和获得时间函数

    关于Linux下时间编程的问题: 1. Linux下与时间有关的结构体 struct timeval { int tv_sec; int tv_usec; }; 其中tv_sec是由凌晨开始算起的秒数 ...

  2. 8. Docker Machine

  3. MySQL之基础功能

    一.视图 视图:是一个虚拟表,其内容由查询定义.同真实的表一样,视图包含一系列带有名称的列和行数据 视图有如下特点: 1. 视图的列可以来自不同的表,是表的抽象和逻辑意义上建立的新关系. 2. 视图是 ...

  4. 使用Opencv时编译错误

    1)无法打开包括文件: “cv.h”: No such file or directory 我的配置文件没有问题,但是一直报错,我是在HEVC测试软件HM中调用了opencv. HM有很多个工程,我只 ...

  5. python中的any和all函数

    any和all函数是判断一组数据真假性的综合结果.以下摘选自Stackoverflow. ------------------ 分割线开始 ----------------- any any will ...

  6. Robot Framework 快速入门

    Robot Framework 快速入门 目录 介绍 概述 安装 运行demo 介绍样例应用程序 测试用例 第一个测试用例 高级别测试用例 数据驱动测试用例 关键词keywords 内置关键词 库关键 ...

  7. jquery通配符说明

    按姓名匹配 1,name前缀为aa的所有div的jquery对象 Js代码 收藏代码$("div[name^='aa']"); 2,name后缀为aa的所有div的jquery对象 ...

  8. (16) go 面向对象

    一.封装 二.继承 1. 2. 3. 4. 5 6. 7.多重继承 三.接口

  9. 2017 计蒜之道 初赛 第五场 A. UCloud 机房的网络搭建

    贪心. 从大到小排序之后进行模拟,注意$n=1$和$n=0$的情况. #include <iostream> #include <cstdio> #include <cs ...

  10. Python并发编程-进程池的返回值

    同步或异步获取返回值 #p = Pool() #p.map(funcname,iterable) 默认异步的执行任务,且自带close,join功能 #p.apply(), 同步调用进程池的方法 #p ...