Command order mode and related C + + implementation _c language in the detailed design pattern

Source: Internet
Author: User

The function of a command pattern is to encapsulate a request into an object, so that you can parameterize the client with different requests, queue or log request logs for requests, and support revocable operations.

Because of the tight coupling between the behavior requester and the behavior performer, use command mode to queue requests or log request logs, and to support revocable operations.

Command mode splits the object that requests an action from the object that knows how to perform an operation.

The key to command mode is to tell a request to be encapsulated into a class (Command), to provide a processing object (Receiver), and the last command command to be activated by Invoker. In addition, we can abstract the processing of the request receiver as a parameter to the command object, which is actually the mechanism of the callback to achieve this. That is, the processing action method address is passed to the Command object by argument, and the command object calls the function at the appropriate time.

Command mode will invoke the object of the operation and the object that knows how to implement the operation, and in the structure diagram of the command above, the Invoker object simply does not know exactly which object is handling the execute operation (which, of course, is the object of the command Class).
It is easy to add new processing objects to the command, and we can do this by creating a new subclass that inherits from the command.
Command mode can be combined with the memento mode to support canceled operations.

Structure Chart:

    • Command class, which declares an interface to perform an action
    • Concretecommand, binds a recipient object to an operation, invokes the corresponding action of the receiver to implement the Execute
    • Invoker class, which requires the command to execute this request
    • Receiver class, knowing how to implement a request-related operation, any class can be a recipient.

Command mode implements the decoupling between the object that invoked the operation and the concrete implementation of the operation by encapsulating the request into an object command and depositing the recipient of the request into a specific Concretecommand class.

In the command pattern map, place the recipient of the request (the processor) in the specific subclass Concretecommand of the command, and when the request arrives (Invoker emits an invoke message to activate the command object), Concretecommand the processing request to the receiver object for processing.


Example:

Namespace Bridge_designpattern {using System;

    Class Abstraction {protected implementation imptouse;
    public void setimplementation (Implementation i) {imptouse = i;          
    Virtual public void dumpstring (String str) {IMPTOUSE.DOSTRINGOP (str); } class Derivedabstraction_one:abstraction {override public void dumpstring (string str) {str +
      = ". com";      
    IMPTOUSE.DOSTRINGOP (str); } class Implementation {public virtual void Dostringop (String str) {Console.WriteLine ("Stand
      ARD Implementation-print string as is ");
    Console.WriteLine ("string = {0}", str);
      Class Derivedimplementation_one:implementation {override public void Dostringop (string str) {
    Console.WriteLine ("Derivedimplementation_one-don ' t print string"); Class Derivedimplementation_two:implementation {override public void Dostringop (String str) {Console.WriteLine ("Derivedimplementation_two-print string Twice");
      Console.WriteLine ("string = {0}", str);
    Console.WriteLine ("string = {0}", str);
  }///<summary>///summary description for Client. </summary> public class Client {abstraction setupmyparticularabstraction () {//We localize To the decision which abstraction and//which implementation to use. These are need to is decided//somewhere and we do. 
      All teh rest of the client/code can work against the abstraction object.
      Abstraction A = new Derivedabstraction_one ();
      A.setimplementation (New Derivedimplementation_two ());
    return A;
      public static int Main (string[] args) {Client c = new Client ();
        
      Abstraction A = C.setupmyparticularabstraction (); From in-here, client code thinks it are talking to the//abstraction, and won't need to be changed as//derived abstractions are changed.

      More client-code using the abstraction goes here//... a.dumpstring ("Clipcode");
    return 0;

 }
  }
}


Advantages of Command mode:
1, it can easily design a command queue;
2, in the case of need, can be easier to log the command;
3, allow the party receiving the request to decide whether to veto the request.
4, the cancellation and redo of the request can be easily realized;
5, it is easy to add new specific command classes because adding new specific command classes does not affect other classes.

Applicable scenario:

    • abstracts the action to be performed to parameterize an object, and you can express this parameterization mechanism using the callback in the procedure language (c a l l b a C K) function. A callback function is when a function is registered somewhere, and it will be invoked at a later time when it is needed. The C o m a n d mode is an object-oriented alternative to the callback mechanism. The
    • specifies, arranges, and executes requests at different times. A c o m m a N object can have a lifetime that is unrelated to the initial request. If the recipient of a request can be expressed in a manner unrelated to the address space, the Command object responsible for the request can be routed to a different process and the request is implemented there. The
    • supports canceling operations. The e x c u T e operation of C o m a n D can store the state before the operation is implemented, and this state is used to eliminate the effect of the operation when the operation is canceled. C o m a N D interface must add a U n e x e c U T e operation, which cancels the effect of the last E x e c U T e call. The commands that are executed are stored in a history list. You can do this by traversing the list backwards and forwards and calling U n e x e c U T e and E x e c U T e to achieve the "undo" and "Redo" of the heavy numbers. The
    • supports modifying logs so that when the system crashes, these changes can be repeated. Add mount operations and storage operations to the C o m a n D interface, which can be used to maintain a consistent change log. The process of recovering from a crash involves reread the recorded commands from disk and then executing them again with E x e c U T e operations. The
    • constructs a system with high-level operations built on primitive operations. Such a structure is common in information systems that support transactions (T r a n s a c t i O). A transaction encapsulates a set of changes to the data. C o m A n d mode provides a way to model transactions. C o m a n D has a common interface so that you can invoke all transactions in the same way. Using this pattern at the same time is also easy to add new transactions to extend the system.

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.