2. 命令(Command)
Intent
将命令封装成对象中,具有以下作用:
- 使用命令来参数化其它对象
- 将命令放入队列中进行排队
- 将命令的操作记录到日志中
- 支持可撤销的操作
Class Diagram
- Command:命令
- Receiver:命令接收者,也就是命令真正的执行者
- Invoker:通过它来调用命令
- Client:可以设置命令与命令的接收者

Implementation
设计一个遥控器,可以控制电灯开关。

1 | public interface Command { |
1 | public class LightOnCommand implements Command { |
1 | public class LightOffCommand implements Command { |
1 | public class Light { |
1 | /** |
1 | public class Client { |