A brief understanding of command mode in C++ design patterns

黄舟
Release: 2017-01-17 13:43:52
Original
1491 people have browsed it

Command mode (Command) encapsulates a request as an object, allowing you to parameterize clients with different requests; queue requests or record request logs, and support reversible 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;
}
Copy after login

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){}
    virtual void ExcuteCommand() = 0;
};

class BakeMuttonCommand :public Command{
public:
    BakeMuttonCommand(Barbecuer & b) { receiver = b; }
    void ExcuteCommand(){ receiver.BakeMutton(); }
};

class BakeChickenWingCommand :public 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();
    }
}
Copy after login

Summary:

It is easier to design a command queue;

It is easier to log commands if necessary;

Allows receiving requests One party decides whether to veto the request;

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

Since adding a new command class does not affect other classes, it is easy to add new specific command classes.

The command mode separates the object that requests an operation from the object that knows how to perform an operation


The above is the content of the command mode of C++ design mode. For more related content, please pay attention to the PHP Chinese website (www.php .cn)!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template