命令模式
定义
命令模式讲请求封装成对象,以便使用不同的请求、队列或者日志来参数化其他对象。命令模式也支持可撤销的操作。
一个命令对象通过在特定接收者上绑定一组动作来封装一个请求。要达到这一点,命令对象将动作和接收者包进对象中。这个对象只暴露出一个execute()方法,当此方法呗调用的时候,接收者就会进行这些动作。从外面来看,其他对象不知道究竟哪个接收者进行了哪些动作,只知道如果调用execute()方法,请求的目的就能达到。
简单命令模式代码示例
package restaurant;
/**
* @ClassName: Command
* @Author: 1
* @Description:
* @Version: 1.0
*/
public interface Command {
public void execute();
}
package restaurant;
/**
* @ClassName: Light
* @Author: 1
* @Description:
* @Version: 1.0
*/
public class Light {
public void on() {
System.out.println("light on");
}
}
package restaurant;
/**
* @ClassName: LightOnCommand
* @Author: 1
* @Description:
* @Version: 1.0
*/
public class LightOnCommand implements Command {
Light light;
public LightOnCommand(Light light) {
this.light = light;
}
@Override
public void execute() {
light.on();
}
}
package restaurant;
/**
* @ClassName: SimpleRemoteControl
* @Author: 1
* @Description:
* @Version: 1.0
*/
public class SimpleRemoteControl {
Command command;
public SimpleRemoteControl() {
}
public void setCommand(Command command) {
this.command = command;
}
public void buttonWasPressed() {
command.execute();
}
}
package restaurant;
/**
* @ClassName: RemoteControlTest
* @Author: 1
* @Description:
* @Version: 1.0
*/
public class RemoteControlTest {
public static void main(String [] args) {
SimpleRemoteControl remote = new SimpleRemoteControl();
Light light = new Light();
LightOnCommand lightOn = new LightOnCommand(light);
remote.setCommand(lightOn);
remote.buttonWasPressed();
}
}
通过运行RemoteControlTest的Main函数得到运行结果
light on
遥控器实现
遥控器有多个按钮,每个按钮对应不同的操作。我们打算将遥控器的每个按钮对应到一个命令,这样就可以让遥控器变成调用者,当按下按钮时,相应命令对象的execute()方法就会被i调用,结果就是接收者的动作被调用。
package remote;
/**
* @ClassName: RemoteControl
* @Author: 1
* @Description:
* @Version: 1.0
*/
public class RemoteControl {
Command[] onCommands;
Command[] offCommands;
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;
}
}
public void setCommand(int slot, Command onCommand, Command offCommand) {
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
public void onButtonWasPushed(int slot) {
onCommands[slot].execute();
}
public void offButtonWasPushed(int slot) {
offCommands[slot].execute();
}
@Override
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.getClass().getName() +
" " + offCommands[i].getClass().getName() + "\n");
}
return stringBuffer.toString();
}
}
package remote;
/**
* @ClassName: NoCommand
* @Author: 1
* @Description:
* @Version: 1.0
*/
public class NoCommand implements Command {
@Override
public void execute() {
}
}
package remote;
/**
* @ClassName: RemoteLoader
* @Author: 1
* @Description:
* @Version: 1.0
*/
public class RemoteLoader {
public static void main(String[] args) {
RemoteControl remoteControl = new RemoteControl();
Light light = new Light();
Command lightOnCommand = new LightOnCommand(light);
Command lightOffCommand = new LightOffCommand(light);
remoteControl.setCommand(0,lightOnCommand,lightOffCommand);
remoteControl.onButtonWasPushed(0);
remoteControl.offButtonWasPushed(0);
}
}
RemoteLoader中,将命令对象通过setCommand方法为每个插槽的按钮绑定事件。通过调用
onButtonWasPushed和offButtonWasPushed来控制第几个按钮的开和关动作。上述代码中,为了不想每次都检查某个插槽是否加载了命令,比如在onButtonWasPushed()方法中我们需要添加验证是否是空
public void onButtonWasPushed(int slot) {
if ( onCommands[slot] != null){
onCommands[slot].execute();
}
}
为了避免上述做法,我们实现了一个不做任何事情的命令,这么一来在RemoteControl构造器中,我们将每个插槽都预先指定NoCommand对象,以便确定每个插槽永远都有对象。所以在测试的输出中,没有被明确指定命令的插槽,其命令将是默认的NoCommand对象。举例来说,遥控器不可能一出厂就设置了有意义的命令对象,所以提供了NoCommand对象作为代用品,当调用他的execute()方法时,这种对象什么事情都不做。在组多设计模式中,都会看到空对象的使用。甚至有些时候,空对象本身也被视为是一种设计模式。
最后我们需要添加对撤销按钮的支持。撤销按钮会将上一个动作取消,例如电灯是关闭的当你按下开启按钮后打开,继续按下撤销按钮时,开启操作应该被撤销,灯应该回到熄灭状态。我们首先将Command接口添加一个撤回的方法
public interface Command {
public void execute();
public void undo();
}
之后在各个命令中实现undo方法,对于撤销来说也就是应该执行与当前命令相反的命令,关闭灯的撤销命令就应该开启灯。
public class LightOffCommand implements Command {
Light light;
public LightOffCommand(Light light) {
this.light = light;
}
@Override
public void execute() {
light.off();
}
@Override
public void undo() {
light.on();
}
}
package remote;
/**
* @ClassName: LightOnCommand
* @Author: 1
* @Description:
* @Version: 1.0
*/
public class LightOnCommand implements Command {
Light light;
public LightOnCommand(Light light) {
this.light = light;
}
@Override
public void execute() {
light.on();
}
@Override
public void undo() {
light.off();
}
}
最后我们要对遥控器做一些小修改。能够让他追踪最后被调用的命令,不管何时撤销按钮被按下我们都可以取出这个命令并调用它的undo方法。
package remote;
/**
* @ClassName: RemoteControl
* @Author: 1
* @Description:
* @Version: 1.0
*/
public class RemoteControlWithUndo {
Command[] onCommands;
Command[] offCommands;
// 记录上一次操作的命令变量
Command undoCommand;
public RemoteControlWithUndo() {
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) {
onCommands[slot] = onCommand;
offCommands[slot] = offCommand;
}
public void onButtonWasPushed(int slot) {
onCommands[slot].execute();
// 每次执行命令的时候都赋值给undoCommand
undoCommand = onCommands[slot];
}
public void offButtonWasPushed(int slot) {
offCommands[slot].execute();
undoCommand = offCommands[slot];
}
public void undoButtonWasPushed() {
undoCommand.undo();
}
@Override
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.getClass().getName() +
" " + offCommands[i].getClass().getName() + "\n");
}
return stringBuffer.toString();
}
}
要点
- 命令模式将发出请求的对象和执行请求的对象解耦。
- 在被解耦的两者之间是通过命令对象进行沟通的,命令对象封装了接收者和一个或一组动作。
- 调用者通过调用命令对象的execute()发出请求,这会使得接受者的动作被调用。
- 调用者可以接受命令当作参数,甚至在运行时动态的进行。
- 命令可以支持撤销,做法是实现一个undo()方法来回到execute()被执行的状态。