一、定义
命令模式就是将一些请求封装为对象,以便使用不同的请求、队列、或者日志来参数化其他对象。命令模式也可以支持撤销的操作。
也就是说可以把一些动作封装为对象,以便于我们随心所欲地存储、传递和调用它们。
这种正式的定义一般都比较抽象的,我们下面通过设计一个遥控器的例子来理解。
二、实现
有这样一个需求,设计一个遥控器,遥控器上有很多插槽,这些插槽可以控制一些电器的开关,并且支持插槽的更换,比如插槽1原本控制灯,需要支持改成控制电视。而且这些电器对象的开和关的方法并不一致,比如灯的开关方法为on和off,电视的开关方法为open和close。
如何实现呢?
- 首先,肯定要解耦,支撑更换插槽这个操作,肯定不能把代码写死,就是我们不能在遥控器中直接调用电器的具体方法。
- 然后,每个电器的开关方法都不一样,而我们遥控器中使用的插槽对象肯定是一致的,所以说需要将这些电器的方法都包装到统一的命令类中。
其实也可以想象一下在饭店中点菜,我们都是在菜单上选中菜品之后,服务员把这个菜单交给厨师去做,我们不会直接和厨师交流,通过菜单实现了解耦。
定义命令接口和默认的无命令类
java
public interface Command {
//执行方法
void execute();
}
public class NoCommand implements Command {
@Override
public void execute() {
System.out.println("没有命令");
}
}
然后,定义具体的命令,每个电器的开和关的命令
java
//两个电器类
//电灯
public class Light {
public void on(){
System.out.println("打开了灯");
}
public void off(){
System.out.println("关闭了灯");
}
}
//电视
public class Television {
public void open(){
System.out.println("打开了电视");
}
public void close(){
System.out.println("关闭了电视");
}
}
//电灯的命令类
public class LightOnCommand implements Command{
private Light light;
public LightOnCommand(Light light) {
this.light = light;
}
@Override
public void execute() {
light.on();
}
}
public class LightOffCommand implements Command{
private Light light;
public LightOffCommand(Light light) {
this.light = light;
}
@Override
public void execute() {
light.off();
}
}
//电视的命令类
public class TelevisionOnCommand implements Command{
private Television television;
public TelevisionOnCommand(Television television) {
this.television = television;
}
@Override
public void execute() {
television.open();
}
}
public class TelevisionOffCommand implements Command{
private Television television;
public TelevisionOffCommand(Television television) {
this.television = television;
}
@Override
public void execute() {
television.close();
}
}
最后,来定义遥控器,这里就以只控制两个电器为例
java
public class RemoteControl {
//默认控制两个电器
private final int size = 2;
private Command[] onCommands;
private Command[] offCommands;
public RemoteControl(){
//创建遥控器时,插槽都是用默认的无命令对象
Command noCommand = new NoCommand();
onCommands = new Command[size];
offCommands = new Command[size];
for (int i = 0; i < size; i++) {
onCommands[i] = noCommand;
offCommands[i] = noCommand;
}
}
public void setCommand(int index, Command onCommand, Command offCommand){
if(index < 0 || index >= size){
throw new RuntimeException("位置错误");
}
onCommands[index] = onCommand;
offCommands[index] = offCommand;
}
public void pressOnButton(int index){
if(index < 0 || index >= size){
throw new RuntimeException("位置错误");
}
onCommands[index].execute();
}
public void pressOffButton(int index){
if(index < 0 || index >= size){
throw new RuntimeException("位置错误");
}
offCommands[index].execute();
}
}
进行测试:
java
public class Test {
public static void main(String[] args) {
test();
}
static void test(){
RemoteControl remoteControl = new RemoteControl();
Light light = new Light();
Television television = new Television();
remoteControl.setCommand(0, new LightOnCommand(light), new LightOffCommand(light));
remoteControl.setCommand(1, new TelevisionOnCommand(television), new TelevisionOffCommand(television));
remoteControl.pressOnButton(0);
remoteControl.pressOnButton(1);
remoteControl.pressOffButton(0);
remoteControl.pressOffButton(1);
}
}
//输出
打开了灯
打开了电视
关闭了灯
关闭了电视
至此,我们通过设计统一的命令接口,将电器和遥控器进行解耦,遥控器不需要管怎么调用电器的开关方法,只需要调用插槽内的命令对象即可,这就是命令模式的简单实现。
三、增加撤销功能
我们想在遥控器上增加一个撤销按钮,按下去之后,就会撤销上一步操作。
其实对于我们现有的命令来说,所谓的撤销就是执行一次相反的操作嘛,比如撤销开灯的操作就需要执行一次关灯,那就可以修改一下Command
接口类,新增一个撤销的方法,让各个命令类去实现具体的撤销操作,同时,遥控器需要保存上一步的操作是什么。
修改Command
接口和各个实现类
java
//命令接口
public interface Command {
//执行方法
void execute();
//撤销
void undo();
}
//电灯
public class LightOffCommand implements Command{
private Light light;
public LightOffCommand(Light light) {
this.light = light;
}
@Override
public void execute() {
light.off();
}
@Override
public void undo() {
light.on();
}
}
public class LightOnCommand implements Command{
private Light light;
public LightOnCommand(Light light) {
this.light = light;
}
@Override
public void execute() {
light.on();
}
@Override
public void undo() {
light.off();
}
}
//电视
public class TelevisionOffCommand implements Command{
private Television television;
public TelevisionOffCommand(Television television) {
this.television = television;
}
@Override
public void execute() {
television.close();
}
@Override
public void undo() {
television.open();
}
}
public class TelevisionOnCommand implements Command{
private Television television;
public TelevisionOnCommand(Television television) {
this.television = television;
}
@Override
public void execute() {
television.open();
}
@Override
public void undo() {
television.close();
}
}
遥控器
java
public class RemoteControl {
//默认控制两个电器
private final int size = 2;
private Command[] onCommands;
private Command[] offCommands;
private Command undoCommand;
public RemoteControl(){
Command noCommand = new NoCommand();
onCommands = new Command[size];
offCommands = new Command[size];
for (int i = 0; i < size; i++) {
onCommands[i] = noCommand;
offCommands[i] = noCommand;
}
undoCommand = noCommand;
}
public void setCommand(int index, Command onCommand, Command offCommand){
if(index < 0 || index >= size){
throw new RuntimeException("位置错误");
}
onCommands[index] = onCommand;
offCommands[index] = offCommand;
}
public void pressOnButton(int index){
if(index < 0 || index >= size){
throw new RuntimeException("位置错误");
}
onCommands[index].execute();
undoCommand = onCommands[index];
}
public void pressOffButton(int index){
if(index < 0 || index >= size){
throw new RuntimeException("位置错误");
}
offCommands[index].execute();
undoCommand = offCommands[index];
}
//撤销操作
public void undo(){
undoCommand.undo();
}
}
测试:
java
public class Test {
public static void main(String[] args) {
test();
}
static void test(){
RemoteControl remoteControl = new RemoteControl();
Light light = new Light();
Television television = new Television();
remoteControl.setCommand(0, new LightOnCommand(light), new LightOffCommand(light));
remoteControl.setCommand(1, new TelevisionOnCommand(television), new TelevisionOffCommand(television));
remoteControl.pressOnButton(0);
System.out.println("撤销上一步");
remoteControl.undo();
remoteControl.pressOnButton(0);
remoteControl.pressOnButton(1);
remoteControl.pressOffButton(0);
System.out.println("撤销上一步");
remoteControl.undo();
remoteControl.pressOffButton(1);
}
}
//输出
打开了灯
撤销上一步
关闭了灯
打开了灯
打开了电视
关闭了灯
撤销上一步
打开了灯
关闭了电视
通过测试我们看到我们成功实现了撤销功能。
四、复杂一点的撤销
上面的电器只是开关这两种完全相反的操作,撤销很容易实现,那如果是电风扇有不同的档位呢?如何实现?其实也很简单,只需要在设置档位的时候,记录下设置之前的档位是什么,撤销操作就是恢复到之前的档位。
并且一定要注意,这个记录操作也是同样放到命令类中的。
java
//风扇类
public class Fan {
public static final int OFF = 0;
public static final int LOWER = 1;
public static final int MID = 2;
public static final int HIGH = 3;
private Integer level;
public Fan() {
this.level = OFF;
}
public Integer getLevel() {
return level;
}
public void turnOFF(){
this.level = OFF;
}
public void turnLower(){
this.level = LOWER;
}
public void turnMid(){
this.level = MID;
}
public void turnHigh(){
this.level = HIGH;
}
}
//风扇的公共抽象命令类
public abstract class FanAbstractCommand implements Command{
private Fan fan;
private Integer lastLevel;
public FanAbstractCommand(Fan fan) {
this.fan = fan;
}
//子类去实现,子类在执行命令之前会记录风扇的当前状态到lastLevel中
public abstract void execute();
//撤销操作,根据记录的lastLevel进行撤销
@Override
public void undo() {
if(lastLevel == null) return;
if(Fan.OFF == lastLevel){
fan.turnOFF();
}else if(Fan.LOWER == lastLevel){
fan.turnLower();
}else if(Fan.MID == lastLevel){
fan.turnMid();
}else if(Fan.HIGH == lastLevel){
fan.turnHigh();
}
}
public Fan getFan() {
return fan;
}
public void setLastLevel(Integer lastLevel){
this.lastLevel = lastLevel;
}
}
//子类实现
public class FanLowerCommand extends FanAbstractCommand{
public FanLowerCommand(Fan fan) {
super(fan);
}
@Override
public void execute() {
Fan fan = getFan();
setLastLevel(fan.getLevel());
fan.turnLower();
}
}
public class FanMidCommand extends FanAbstractCommand{
public FanMidCommand(Fan fan) {
super(fan);
}
@Override
public void execute() {
Fan fan = getFan();
setLastLevel(fan.getLevel());
fan.turnMid();
}
}
//省略其他两个状态的子类
直接测试
java
public class Test {
public static void main(String[] args) {
testFan();
}
static void testFan(){
RemoteControl remoteControl = new RemoteControl();
//把0号插槽的开启位设置为低速,关闭位设置为关闭
//把1号插槽的开启位设置为中速,关闭位设置为高速
Fan fan = new Fan();
remoteControl.setCommand(0, new FanLowerCommand(fan), new FanOffCommand(fan));
remoteControl.setCommand(1, new FanMidCommand(fan), new FanHighCommand(fan));
//测试
remoteControl.pressOnButton(1);
remoteControl.pressOffButton(1);
System.out.println("撤销上一步");
remoteControl.undo();
}
}
//输出结果
风扇中速
风扇高速
撤销上一步
风扇中速
五、宏命令
现在的智能家居很多支持自定义宏,比如一键开启观影模式,就会打开电视、打开印象、关闭窗帘等一系列操作,我们有了命令模式要实现这种宏命令就很简单了。
你可能会想,直接定义一个命令,在这个命令的execute
方法中直接调用一系列电器,这种方法可以实现,但是过于死板,比如我想在更改这个宏命令就只能修改代码,所以我们需要定义一个通用的宏命令,这个宏命令中存储一系列命令就好啦。
java
public class MacroCommand implements Command{
private final List<Command> commands;
public MacroCommand(List<Command> commands) {
this.commands = commands;
}
@Override
public void execute() {
for (Command command : commands) {
command.execute();
}
}
@Override
public void undo() {
for (Command command : commands) {
command.undo();
}
}
}
测试:
java
public class Test {
public static void main(String[] args) {
testMacro();
}
static void testMacro(){
RemoteControl remoteControl = new RemoteControl();
Light light = new Light();
Television television = new Television();
Fan fan = new Fan();
List<Command> onCommands = Arrays.asList(new LightOnCommand(light),new TelevisionOnCommand(television),new FanLowerCommand(fan));
MacroCommand macroOnCommand = new MacroCommand(onCommands);
List<Command> offCommands = Arrays.asList(new LightOffCommand(light),new TelevisionOffCommand(television),new FanOffCommand(fan));
MacroCommand macroOffCommand = new MacroCommand(offCommands);
remoteControl.setCommand(0,macroOnCommand,macroOffCommand);
System.out.println("===一键开启宏命令===");
remoteControl.pressOnButton(0);
System.out.println("===一键关闭宏命令===");
remoteControl.pressOffButton(0);
System.out.println("===撤销上一步操作===");
remoteControl.undo();
}
}
//输出结果
===一键开启宏命令===
打开了灯
打开了电视
风扇低速
===一键关闭宏命令===
关闭了灯
关闭了电视
关闭风扇
===撤销上一步操作===
打开了灯
打开了电视
风扇低速