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, TV, remote control and the relationship between the receiver
2. Change the light and dark level of a view (no command mode is used)
3. Introduction of Command mode
4. Change the light and dark level of a view (using command mode)
5. Appendix
Command mode 1. The relationship between the TV, the remote control and the receiver
The TV has no remote, and of course it can change.
If you want to use the remote control, the TV must have an infrared receiver, specifically to accept the remote control information.
The infrared receiver specially accepts the remote control information and transforms it into the information inside the TV set to allow the TV to recognize.
- 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)
First add three buttons to the Viewcontroller and set the relevant properties and listen for the Click events:
Then create the task performer object receiver class, which creates an instance object responsible for performing the adjustment of the view background bright darkness
Because in the non-command mode, the revocation logic is not implemented, here does not provide the implementation of the
Last Show Effect:
Actually here, the receive object is equivalent to Viewcontroller agent, the agent completes the control belongs to the Viewcontroller management and the control view background bright darkness adjusts. It's just that the agent is not so abstract and does not follow an agreement. is the specific and direct completion of the logical business. This simple pattern can be used in projects that do not consider extensibility or that a module function is fixed.
However, some business requirements will need to record the status of storage and removal of execution tasks or information delivery commands, like here if you want to add undo operation, you need to record the previous operation, and then according to the previous action fallback in turn, then, the command mode can be more convenient to implement this logic. The command mode is actually to abstract the sent command information into a class, and then the specific information to create a specific class, and through the callback to add and execute Command object execution commands involved in the task method, while storing records this command, then because each operation can be stored down, so it is easy to design the undo operation. This is an advantage of the command pattern.
3. Command mode Introduction
UML diagram for Command mode:
Overview
在软件系统中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”。但在某些场合,比如要对行为进行“记录、撤销/重做、事务”等处理,
这种无法抵御变化的紧耦合是不合适的。在这种情况下,如何将“行为请求者”与“行为实现者”解耦?将一组行为抽象为对象,实现二者之间
的松耦合。这就是命令模式(Command Pattern)。
4. Change the light and dark level of a view (using command mode)
Analyze the previous code:
为了能够行为进行“记录、撤销/重做、事务”等处理,我们可以使用命令模式:
- Set the recipient
- Abstract the action of changing shading into an object
- Undo Action
Analysis:
The initiator of the final command and the executor of the command are decoupled.
Provide a more complete code link: http://pan.baidu.com/s/1sjOlm8l Password: me5f
5. Appendix
iOS Command mode learning site: http://www.cnblogs.com/YouXianMing/p/4888402.html
Great God Project source code programming details learning accumulation, so-called programming experience is in the actual code application, code usage, code style, code presentation logic Way, analysis and accumulation of real years of experience in the development of the great God written code, you can improve the programming experience faster: Use the enumeration to set the tag value of the control: Draw the border properties of the control: Command mode code details: set min and Max boundaries with built-in functions, the individual feels redundant here, but the usage of these two functions can be accumulated and used for other needs. Tips for Learning programming details: null references and Empty methods:
iOS design mode-command mode