命令模式将”请求“封装成对象,以便使用不同的请求、队列、或者日志来参数化其他对象。命令模式也可支持可撤销的操作。
一个命令对象通过在特定接收者上绑定一组动作来封装一个请求。也就是说:命令对象将动作和接收者包进对象中,这个对象只暴露出一个execute方法,当execute方法被调用的时候,接收者就会进行这些动作,从外面来看,其他对象不知道究竟哪个接收者进行了哪些动作,只知道调用了execute方法目的就达到了。
命令模式类图如图所示:
Client:负责创建一个ConcreteCommand,并设置其接收者。
Invoker:这个调用者持有一个命令对象,并在某个时间点调用命令对象的execute()方法,将请求付诸行动。
Command为所有命令声明了一个接口,调用命令对象的execute方法,就可以让接收者进行相关动作。
ConcreteCommand: 该类定义了动作和接收者之间的绑定关系。调用者只要调用execute接口就可以发出请求,然后由ConcreteCommand调用接收者的一个或多个动作。
Receiver:接收者知道如何进行必要的工作,实现这个请求。任何类都可以当接收者。
代码示例:
receiver:
public interface Light { public void on(); public void off(); }
public class KitchenLight implements Light{ public void on(){ System.out.println("KitchenLight light on."); } public void off(){ System.out.println("KitchenLight light off."); } }
public class LivingRoomLight implements Light{ public void on(){ System.out.println("LivingRoomLight light on."); } public void off(){ System.out.println("LivingRoomLight light off."); } }
public class GarageDoor { public void open(){ System.out.println("This door is open"); } public void close(){ System.out.println("This door is closed"); } }
Command:
public interface Command { public void execute(); }
ConcreteCommand:
public class LightOnCommand implements Command { Light light; public LightOnCommand( Light light){ this.light = light; } @Override public void execute() { light.on(); } }
public class LightOffCommand implements Command { Light light ; public LightOffCommand(Light light){ this.light = light; } @Override public void execute() { light.off(); } }
public class GarageDoorOpen implements Command { GarageDoor garageDoor; public GarageDoorOpen(GarageDoor garageDoor){ this.garageDoor = garageDoor; } @Override public void execute() { garageDoor.open(); } }
public class GarageDoorClose implements Command { GarageDoor garageDoor; public GarageDoorClose(GarageDoor garageDoor){ this.garageDoor = garageDoor; } @Override public void execute() { garageDoor.close(); } }
Invoker:
public class SimpleRemoteControl { Command slot; public SimpleRemoteControl(){} public void setCommand(Command command){ this.slot = command; } public void buttonWasPressed(){ slot.execute(); } }
Client:
public class RemoteControlTest { public static void main(String[] args){ SimpleRemoteControl remote = new SimpleRemoteControl(); LightOnCommand lightOnCommand = new LightOnCommand(new LivingRoomLight()); remote.setCommand(lightOnCommand); remote.buttonWasPressed(); lightOnCommand = new LightOnCommand(new KitchenLight()); remote.setCommand(lightOnCommand); remote.buttonWasPressed(); GarageDoorOpen garageDoorOpen = new GarageDoorOpen(new GarageDoor()); remote.setCommand(garageDoorOpen); remote.buttonWasPressed(); } }
测试结果:
LivingRoomLight light on.
KitchenLight light on.
This door is open