C + + design mode shallow command mode

Source: Internet
Author: User
The command mode 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.

Test Case:

[Code]int Main () {    Barbecuer boy;    Bakechickenwingcommand bakechickenwingcommand1 (boy);    Bakechickenwingcommand bakechickenwingcommand2 (boy);    Bakemuttoncommand bakemuttoncommand1 (boy);    Bakemuttoncommand bakemuttoncommand2 (boy);    Waiter girl;    Girl. Setorder (&BAKECHICKENWINGCOMMAND1);    Girl. Setorder (&BAKECHICKENWINGCOMMAND2);    Girl. Setorder (&BAKEMUTTONCOMMAND1);    Girl. Setorder (&BAKEMUTTONCOMMAND2);    Girl. Notify ();    Girl. Cancelorder (&BAKECHICKENWINGCOMMAND2);    Girl. Notify ();    return 0;}

Class implementation:

[Code]class barbecuer{public:void Bakemutton () {cout << "meat\n";    } void Bakechickenwing () {cout << "chicken\n"; }};class Command{protected:barbecuer Receiver;public:command () {} Command (Barbecuer & B): Receiver (b) {} V irtual void Excutecommand () = 0;};    Class Bakemuttoncommand:p ublic Command{public:bakemuttoncommand (Barbecuer & B) {receiver = B;} void Excutecommand () {receiver. Bakemutton ();    }};class Bakechickenwingcommand:p ublic command{public:bakechickenwingcommand (Barbecuer & B) {receiver = B;} void Excutecommand () {receiver. Bakechickenwing ();    }};class waiter{list<command *>orders;public:void setorder (Command * comptr);    void Cancelorder (Command * comptr); void Notify ();};    void Waiter::setorder (Command * comptr) {orders.push_back (comptr); cout << "Add order\n";}    void Waiter::cancelorder (Command * comptr) {orders.remove (comptr); cout << "Cancel order\n";} VOID Waiter::notify () {for each (Command * var in orders) {Var->excutecommand (); }}

Summarize:

Easier to design a command queue;

It is easier to log the command in case of need;

Allow the party receiving the request to decide whether to veto the request;

The revocation and redo of the request can be implemented easily;

Because the addition of new command classes does not affect other classes, it is easy to add new specific command classes.

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


The above is the C + + design mode of the simple command mode of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!


  • Related Article

    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.