Command mode--design mode (18)

Source: Internet
Author: User

Memento mode Source:

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 requested recipient in the specific Concretecommand class (Receiver).

Memento Mode Effect:

Encapsulates a request as an object so that you can parameterize the customer with different requests, queue 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.


The memento mode UML structure is shown in Figure 1:

< Span style= "font-family: ' Microsoft Yahei '; font-size:15px; line-height:35px ">                   and nbsp  

< Span style= "font-family: ' Microsoft Yahei '; font-size:15px; line-height:35px ">memento mode mode composition:

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.

< Span style= "font-family: ' Microsoft Yahei '; font-size:15px; line-height:35px "> memento pattern code example:

Command.h

#ifndef _command_h_#define _command_h_class command{public:    virtual ~command ();    virtual void Execute () =0;protected:    Command ();p rivate:};class receiver;class concretecommand:public command{ Public:    Concretecommand (receiver* preceiver);    ~concretecommand ();    virtual void Execute ();p rotected:private:    receiver* _recv;}; Class Invoker{public:    Invoker (command* pcommand);    ~invoker ();    void Invoke ();p rotected:private:    command* _cmd;}; Class Receiver{public:    Receiver ();    ~receiver ();    void Action ();p rotected:private:}; #endif

Command.cpp

#include "Command.h" #include <iostream>using namespace std; Command::command () {}command::~command () {}concretecommand::concretecommand (receiver* pReceiver) {    this->_ recv = Preceiver;} Concretecommand::~concretecommand () {}void concretecommand::execute () {    this->_recv->action ();} Receiver::receiver () {}receiver::~receiver () {}void receiver::action () {    cout << "Receiver::action" < < Endl;} Invoker::invoker (command* pcommand) {    this->_cmd = pcommand;} Invoker::~invoker () {}void invoker::invoke () {    this->_cmd->execute ();}

Main.cpp

#include "Command.h" int main () {    //Create a specific Command object Pcmd and set its receiver PRev    receiver* PRev = new Receiver ();    command* pcmd = new Concretecommand (PRev);    Request Binding Command    invoker* PINV = new Invoker (pcmd);    Pinv->invoke ();    return 0;}

Command mode applicability:

You should consider using the command pattern in the following situations:

(1). Use the command mode as an alternative to "CallBack" in an object-oriented system. The "CallBack" is about registering a function first, and then calling this function later.

(2). You need to specify the request at a different time and queue the request. A Command object and the original request issuer can have different lifetimes. In other words, the original request issuer may already be gone, and the command object itself is still active. At this point the recipient of the command can be either local or another address on the network. The command object can be routed to another machine after serialization.

(3). The system needs to support undo for the command. The Command object can store the state and wait until the client needs to undo the effect of the command, and the Undo () method can be called to undo the effect of the command. The Command object can also provide the redo () method for the client to re-enforce the command effect when needed.

(4). If a system is going to update all the data in the system to the log, so that when the system crashes, it can read back all the data update commands in the log, and call the Execute () method one at a time to execute the commands to restore the data updates that the system made before the crash.

Command mode pros and cons:

Advantages of the Command mode:
(1). It can design a command queue more easily;
(2). In case of need, it can be easier to log the command;
(3). Allow the party receiving the request to decide whether to veto the request.
(4). The revocation and redo of the request can be achieved easily;
(5). Because adding new specific command classes does not affect other classes, it is easy to add new specific command classes.

Disadvantages of the command pattern:

(1). Using command mode may cause some systems to have too many specific command classes. Because you need to design a specific command class for each command, some systems may require a large number of specific command classes, which will affect the use of the command pattern.

Command mode Usage Summary:

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.


Command mode--design mode (18)

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.