Command mode
Objective:
The Command object encapsulates information about how the instruction is executed against the target, so the client or caller does not have to know any details of the target, but can still perform any existing operations on him. By encapsulating the request into an object, the client can parameterize it and place it in a queue or log, and can support a revocable operation. The command object binds one or more actions to a specific sink. The command pattern eliminates the binding between the action as an object and the receiver that executes it.
Body Content outline:
1、电视机、遥控器与接收器之间的关系2、改变一个视图的明暗程度(未使用命令模式)3、命令模式介绍4、改变一个视图的明暗程度(使用命令模式)5、附录
Command mode 1. The relationship between the TV, the remote control and the receiver
电视机没有遥控器当然也能够换台如果要用遥控器,电视机上面肯定要有红外线接收器,专门用来接受遥控器的信息。红外线接收器专门接受遥控器的信息,并转化成电视机里面的信息,让电视机识别。
- Devices not necessary for remote control and receivers
- Receiver converts signal from remote control
- TV, remote control and receiver relationship: TV can be independent, without remote control and infrared receiver
2. Small instance item that changes the shading of a view (not using command mode)
3. Command mode Introduction
UML diagram for Command mode:
Overview
在软件系统中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”。但在某些场合,比如要对行为进行“记录、撤销/重做、事务”等处理,这种无法抵御变化的紧耦合是不合适的。在这种情况下,如何将“行为请求者”与“行为实现者”解耦?将一组行为抽象为对象,实现二者之间的松耦合。这就是命令模式(Command Pattern)。
4. Change the intensity of a view (not using command mode)
- Set the recipient
- Abstract the action of changing shading into an object
- Undo Action
Code:
The initiator of the command and the executor of the command are decoupled.
Provide more complete code
5. Appendix
iOS Command mode learning site: http://www.cnblogs.com/YouXianMing/p/4888402.html
iOS design mode-command mode