07@pattern_note_ Command mode

Source: Internet
Author: User

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
 
   
  
  1. abstract class Command{
  2. public void execute() {}
  3. }
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.