Command mode (c + + implementation)

Source: Internet
Author: User

The main reference is "Big talk design mode" and "design pattern: The basis of reusable object-oriented software" two books. This article describes the implementation of the command pattern.

What it is:encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

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. In Oop, everything is an object, encapsulates the request into an object, conforms to the design idea of OOP, when a client's individual request is encapsulated into an object, we can store more information about the request, make the request more capable, and the command pattern can also decouple the request sender from the receiver. Allows the command sender to not care about how the request will be handled.

Command: The interface that declares the execution of the operation;
Concretecommand: Binds a Recipient object to an action, and then invokes the corresponding action of the receiver to implement execute to complete the corresponding command;
Client: Creates a specific command object, but does not set its recipient;
Invoker: Requires the command to execute the request;
Receiver: Knowing how to implement the actions associated with executing a request, any class may act as a recipient.

These objects are coordinated in the following ways:

1.Client Create a Concretecommand command object and specify its receiver object;
The 2.Invoker object stores the Concretecommand object;
3. The invoker submits a request by invoking the Execute operation of the Command object. If the command request is revocable, Concretecommand stores the current state before executing the Execute operation to cancel the command request;
The 4.ConcreteCommand object invokes some of the operations of receiver to execute the request.

Specific examples:

We go to the restaurant to eat, we are through the waiter to order, specifically who to do these dishes and when they finished these dishes, in fact, we do not know. Abstract, we are the "menu requestor ", the chef is "food list Realization ",2 is loosely coupled, we have some other requests for these dishes such as "undo, redo" and so on, we do not know who is doing. In fact, this is the command mode to be said in this article . encapsulates a request as an object so that you can parameterize the customer with different requests, queue requests or log requests, and support undoable operations. [GOF "design mode"]

The UML diagram is as follows:

Specific code C + + code implementation:

1#include <iostream>2#include <string>3#include <vector>4 using namespacestd;5 6 #defineSafe_delete (P) if (p) {DELETE p; p = NULL;}7 8 classClient9 {};Ten /*Roast Meat Chef class, only responsible for baking string work*/ One classBarbecuer A { -  Public: -     voidBakemutton () {cout<<"Bake Mutton"<<Endl;} the     voidBakechickenwing () {cout<<"Bake chickenwing"<<Endl;} - }; -  - /*abstract Command class: an interface to perform specific operations*/ + classCommand - { +  Public: A Command () {} atCommand (Barbecuer *receiver):p _receiver (receiver) {} -     Virtual voidExecuteCommand () =0;//Execute Command - protected: -Barbecuer *P_receiver; - }; -  in /*specific command class: Kebabs command*/ - classBakemuttoncommand: PublicCommand to { +  Public: -Bakemuttoncommand (Barbecuer *receiver) {p_receiver =receiver;} the     voidExecuteCommand () {p_receiver->Bakemutton ();} * }; $ Panax Notoginseng /*specific command class: Grilled chicken Wing string Command*/ - classBakechickenwingcommand: PublicCommand the { +  Public: ABakechickenwingcommand (Barbecuer *receiver) {p_receiver =receiver;} the     voidExecuteCommand () +{p_receiver->bakechickenwing ();} - }; $  $ /*Waiter Class*/ - classWaiter - { the  Public: -     voidSetorder (Command *command);Wuyi     voidNotify (); the Private: -Vector<command *>p_commandlist;//This is the equivalent of a Command object queue Wu }; -  About voidWaiter::setorder (Command *command) $ { - p_commandlist.push_back (command); -cout <<"Add Barbecue order"<<Endl; - } A  + voidwaiter::notify () the { -Vector<command*>:: iterator i; $      for(i = P_commandlist.begin (); I! = P_commandlist.end (); + +i) the(*i)ExecuteCommand (); the } the  the intMainintargcChar*argv[]) - {   in     //Generate barbecue chef, waiter, order object theBarbecuer *p_cook =NewBarbecuer (); theCommand *p_mutton =NewBakemuttoncommand (p_cook); AboutCommand *p_chickenwing =NewBakechickenwingcommand (p_cook); theWaiter *p_waiter =Newwaiter (); the  the     //Push the Order object to the command queue +P_waiter->Setorder (P_mutton); -P_waiter->Setorder (p_chickenwing); the Bayi     //The waiter informs the roast chef of the specific order theP_waiter->Notify (); the  - Safe_delete (p_cook); - Safe_delete (P_mutton); the Safe_delete (p_chickenwing); the Safe_delete (p_waiter); the  the     return 0;  -}

Command mode (c + + implementation)

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.