C + + design mode-command Command mode

Source: Internet
Author: User
Tags prev

Command mode
role : Encapsulates a request as an object, allowing you to parameterize the customer with different requests, queue up requests or log requests, and support revocable operations.

Because of the tight coupling between the "behavior requestor" and the "behavior implement", the command pattern can be used to queue requests or log requests, and to support revocable operations.

UML diagram:


Command class, which declares an interface to perform an operation

Concretecommand, binds a recipient object to an operation, invokes the corresponding action of the receiver to implement execute

The Invoker class, which requires the command to execute this request

receiver class, knowing how to implement and execute a request-related operation, any class may act as a recipient.

The command pattern enables decoupling between the object that invokes the operation and the specific implementation of the operation by encapsulating the request into an object command and storing the recipient of the request in the specific Concretecommand class.

command pattern structure diagram, put the receiver (processor) of the request into the specific subclass of command Concretecommand, when the request arrives (Invoker issue the invoke message activates the command object), Concretecommand the processing request to the receiver object for processing.

Advantages of the command mode:
1, it can design a command queue more easily;
2, in the case of need, it can be easier to record the command into the log;
3, allows the party receiving the request to decide whether to veto the request.
4, can easily achieve the revocation and redo of the request;
5, because the addition of the new specific command class does not affect other classes, it is easy to add a new specific command class.

The command mode splits the object that requests an action with the object that knows how to perform an operation.

The key to command mode is to say that a request is encapsulated in a class (Command), and then a processing object (Receiver) is provided, and the last Command command is activated by Invoker. In addition, we can abstract the processing of the request recipient 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.

The command mode decouples the object that invokes the operation and the object that knows how to implement it, and in the above command's structure diagram, the Invoker object does not know exactly which object is handling the execute operation (and of course the object that is the command category).
In order to add new processing operations to the object is easy, we can do this by creating a new subclass that inherits from command.
The command mode can be combined with the memento mode to support canceled operations.

The code is as follows:

Command.h

1 #ifndef _command_h_ 2 #define _COMMAND_H_ 3  4 class COMMAND 5 {6 public:7     virtual ~command (); 8     Virtual Vo ID Execute () = 0; 9 protected:10     Command (); Private:12};13 class receiver;15 class Concretecommand:public Command17 {publi C:19     Concretecommand (receiver* preceiver);     ~concretecommand ();     virtual void Execute (); protected : private:24     receiver* _recv;25};26 class Invoker28 {public:30 Invoker     (command* pcommand);     Invoker (), +     void Invoke (), protected:34 private:35     command* _cmd;36};37, class Receiver39 {public:41< C13/>receiver ();     ~receiver ();     void Action (); protected:45 private:46};47 #endif

Command.cpp

1 #include "Command.h" 2 #include <iostream> 3  4 using namespace std; 5  6 Command::command () 7 {} 8  9 Co Mmand::~command () {}11 Concretecommand::concretecommand (receiver* preceiver) (     this->_recv) Preceiver;15}16-Concretecommand::~concretecommand () {}19-void Concretecommand::execute () +     this->_ Recv->action ();}24 Receiver::receiver () 32 {}27 receiver::~receiver () {}30 to void receiver::action C6/>cout << "receiver::action" << endl;34}35 invoker::invoker (command* pcommand) Panax Notoginseng this->_     cmd = pcommand;39}40 invoker::~invoker () invoker::invoke {}43 this->_cmd->execute () () () (     47}

Main.cpp

1 #include "Command.h" 2  3 int main () 4 {5     //Create a specific Command object Pcmd and set its recipient PRev 6     receiver* PRev = new Receiver (); 7
    command* pcmd = new Concretecommand (PRev); 8     //Request Binding Command 9     invoker* PINV = new Invoker (pcmd);     Pinv->invoke ();     return 0;13}

C + + design mode-command 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.