Objective
20160109: Today begins to look at the command mode, mainly from the concept and implementation to deeply understand the pattern
Concept understanding "part from excerpt" Concept
Generally, the "behavior requestor" is tightly coupled with the "behavior realization person". However, in some cases, such as "record, Undo/Redo, transaction" and so on, this can not resist the change of tight coupling is inappropriate. In these cases, it is important to decouple the "behavior requestor" from the "behavior-implementing person" to achieve the loose coupling between them. Command mode is a good way to solve this kind of problem.
In LabVIEW, the typical producer consumer is the behavior request and the behavior implementation, the two are decoupled by the way of the queue, send the instruction in the form of the string. However, in the design of these operations, the special detection function can not be decoupled, so there is a need to learn.
Command mode
The command pattern encapsulates a request as an object, allowing you to parameterize the customer with different requests, queue requests or log requests, and support actions that can be undone.
The structure diagram of the command pattern is as follows
The AF structure of LabVIEW is to change the command to an object, not knowing if the design method is referenced.
command defines the interfaces of the commands
The message class in LabVIEW
The Concretecommand implements the command interface and defines the specific commands
Message's do, commands are implemented
The client is used to create a specific command and set the recipient
Invoker requires command to execute corresponding request
Receiver implementation and execution of a request, any class may act as receiver
C # Implementation Demo One: the command mode paradigm in design mode implements the command interface
abstract class Command{
public void execute() {}
}
Achieve control
Through the preliminary writing, it can be found that the program changes a single command to the intermediate transfer of the command, the previous need to control is the event structure to send instructions, and then execute a specific piece of code, now encapsulated, you can reuse the execution of the Code;
Implementing the remote Control
Control implementation
Note that there is a coupling problem with my design here
I have included the Lightcommand design with Light.on and Light.off, so in this case, the command has been designed in the way of inheritance, when the increase in commands, and the first chapter of the duck design, inheritance will bring a lot of inconsistency problems, So it's better to use combinatorial design here.
Design in the original book
Redesign, full functionality, but command with a single inheritance (using the Strategy model development, no use of inherited extensibility), so the subsequent changes will be easier to achieve the decoupling of control; in a word, multi-use combination (strategy), less inheritance!
From for notes (Wiz)
[email protected]_note_ Command mode