[C ++ design mode] command mode

Source: Internet
Author: User

[C ++ design mode] command mode

In software systems, "behavior requestor" and "behavior implementers" are usually "tightly coupled ". However, in some cases, such as "record, undo/Redo, and transaction" operations, it is not suitable for tight coupling that cannot withstand changes. In this case, how can we decouple "behavior requestor" from "behavior implementer? Abstract A group of behaviors as objects to achieve loose coupling between them. This is the Command Pattern ).

In OOP, everything is an object. Requests are encapsulated into objects. In line with the OOP design philosophy, after a single request is encapsulated into an object, we can store more information for this request so that the request has more capabilities. The command mode can also decouple the request sender and receiver, so that the command Sender does not have to worry about how the request will be processed.

 

Command:
Define the command interface and declare the execution method.
ConcreteCommand:
The implementation object of the command interface is a "virtual" implementation. It usually holds the receiver and calls the receiver's function to complete the operation to be executed by the command.
Aggreger:
Recipient, the object that actually executes the command. Any class may become a receiver, as long as it can implement the corresponding functions required by the command.
Invoker:
The command object is required to execute the request. Generally, the command object is held and many command objects can be held. This is the place where the client actually triggers the command and requires the command to execute the corresponding operation, that is, it is equivalent to the entry of the command object.
Client:
Create a specific command object and set the receiver of the command object. Note that this is not a Client in the general sense, but an assembly of command objects and recipients. It may be better to call this Client an assembler, because the client that actually uses the command triggers the execution from Invoker.

1. The essence of the command mode is to encapsulate the command and separate the responsibility for issuing the command from that for executing the command.
2. Each Command is an operation: the Requesting Party sends a request and requires an operation. The receiving party receives the request and performs the operation.
3. the command mode allows the requester and the receiver to be independent, so that the requester does not have to know the interfaces of the receiver, or how the request is received, and whether the operation is executed, when it is executed, and how it is executed.
4. The command mode makes the request itself an object, which can be stored and transmitted like other objects.
5. The key to the command mode is to introduce the abstract command interface, and the sender program the abstract command interface. Only specific commands that implement the abstract command interface can be associated with the receiver.

 

class Receiver{public:     void Action()     {          cout<
 
  Action<
  
   Action();     }private:     Receiver *m_pReceiver;};class Invoker{public:     Invoker(Command *pCommand) : m_pCommand(pCommand){}     void Invoke()     {          m_pCommand->Execute();     }private:     Command *m_pCommand;};int main(){     Receiver *pReceiver = new Receiver();     Command *pCommand = new ConcreteCommand(pReceiver);     Invoker *pInvoker = new Invoker(pCommand);     pInvoker->Invoke();     SAFE_DELETE(pInvoker);     SAFE_DELETE(pCommand);     SAFE_DELETE(pReceiver);     return 0;}
  
 


 

 

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.