五、命令模式
1、模式结构及结构图
模式结构:
1.1 命令抽象类(Command):声明执行命令的接口,具有命令执行的抽象方法execute();
1.2 具体命令类(concrete Command): 实现命令接口。它具有接收者对象,并调用接收者对象的功能方法来完成命令的执行。
1.3 接收者对象(Recevicer): 执行命令相关的操作,是具体命令对象的真正实现者。比如接收关灯命令,实际完成断电操作。
1.4 调用者/请求者(Invoker):命令的发送者。具有命令对象,通过命令对象来发送请求。
结构图:
或者:
2、代码实战
需求描述:设置一个灯具开关系统,要求按下开关时,灯具根据当前状态来自动判断是开灯还是光灯。
2.1 命令抽象接口
package command.example;
/**
* 命令指令
*
* @author 刀刀和阳
*/
public interface Command {
void execute();
}
2.2 命令的实现接口
package command.example;
/**
* 灯具命令
*
* @author 刀刀和阳
*/
public class LightCommand implements Command{
Light light;
public LightCommand(Light light) {
this.light = light;
}
@Override
public void execute() {
light.action();
}
}
2.3 命令的真正执行者–灯具本身
package command.example;
/**
* 命令的执行者
* @author 刀刀和阳
*/
public class Light {
/**
* 状态:0 关闭, 1 开启
*/
private int status = 0;
/**
* 灯名
*/
private String name = "";
public Light(String name) {
this.name = name;
}
public void action() {
if (status == 0) {
System.out.println(name + " : 开灯!");
status = 1;
} else {
System.out.println(name + " : 关灯!");
status = 0;
}
}
}
1.4 命令的发送者–灯具开关
package command.example;
/**
* 开关,命令发送者
*
* @author 刀刀和阳
*/
public class Button {
Command command;
public Button(Command command) {
this.command = command;
}
/**
* 按下开关
*/
public void pressButton() {
command.execute();
}
}
1.5 客户端–人类
package command.example;
/**
* 客户端
*
* @author 刀刀和阳
*/
public class Person {
public static void main(String[] args) {
Light light = new Light("厨房");
LightCommand lightCommand = new LightCommand(light);
Button button = new Button(lightCommand);
for (int i = 0; i < 10; i++) {
button.pressButton();
}
}
}
1.6 打印结果
厨房 : 开灯!
厨房 : 关灯!
厨房 : 开灯!
厨房 : 关灯!
厨房 : 开灯!
厨房 : 关灯!
厨房 : 开灯!
厨房 : 关灯!
厨房 : 开灯!
厨房 : 关灯!
3、总结
命令模式的主要优点如下。
- 通过引入中间件(抽象接口)降低系统的耦合度。
- 扩展性良好,增加或删除命令非常方便。采用命令模式增加与删除命令不会影响其他类,且满足“开闭原则”。
- 可以实现宏命令。命令模式可以与组合模式结合,将多个命令装配成一个组合命令,即宏命令。
- 方便实现 Undo 和 Redo 操作。命令模式可以与后面介绍的备忘录模式结合,实现命令的撤销与恢复。
- 可以在现有命令的基础上,增加额外功能。比如日志记录,结合装饰器模式会更加灵活。
其缺点是:
- 可能产生大量具体的命令类。因为每一个具体操作都需要设计一个具体命令类,这会增加系统的复杂性。
- 命令模式的结果其实就是接收方的执行结果,但是为了以命令的形式进行架构、解耦请求与实现,引入了额外类型结构(引入了请求方与抽象命令接口),增加了理解上的困难。不过这也是设计模式的通病,抽象必然会额外增加类的数量,代码抽离肯定比代码聚合更加难理解。
4、进阶