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)!