Big talk Design Mode C ++ implementation-Chapter 2-command mode

Source: Internet
Author: User

Big talk Design Mode C ++ implementation-Chapter 2-command mode

I. UML diagram

 

Ii. Concepts

Command mode ):Encapsulate a request as an object so that you can parameterize the customer with different requests, queue the request or record the request log, and support auditable operations.

 

Iii. Description

Role:

(1) Command class: The interface used to declare the operation to be executed.

(2) ConcreteCommand class: binds a receiver object to an action, and calls the corresponding operation of the receiver to implement Excute.

(3) Invoker class: this command is required to execute this request.

(4) Explorer class: knows how to implement and execute a request-related operation. Any class may act as a Receiver.

What is the role of the command mode?

(1) The command mode can easily design a command queue.

(2) You can easily log commands as needed.

(3) The requesting party is allowed to decide whether to reject the request.

(4) The request can be easily revoked and redone.

(5) It is easy to add a new command class because adding a new command class does not affect other classes.

(6) The command mode separates the objects requesting an operation from those that know how to execute an operation.

 

IV. C ++ implementation

(1) Command. h

 

# Ifndef COMMAND_H # define COMMAND_H # include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
// Else Er: class Barbecuer {public: void BakeMutton () {std: cout <"kebab! "<
     
      
Extends ER = receiver;} virtual void ExcuteCommand () = 0 ;}; // ConcreteCommand class, specific Command class BakeMuttonCommand: public Command {public: BakeMuttonCommand (Barbecuer * receiver ): command (Cycler) {} void ExcuteCommand () {Cycler-> BakeMutton () ;}; // ConcreteCommand class, specific Command class BakeChickenWingCommand: public Command {public: bakeChickenWingCommand (Barbecuer * receiver): Command (guest ER) {} void ExcuteCommand () {guest er-> BakeChickenWing () ;}; // Invoker: here is the Waiter class Waiter {private: std: vector
      
        * Orders; public: Waiter () {orders = new std: vector
       
         ;}~ Waiter () {delete orders;} // set the order void SetOrder (Command * command) {// determine the command type and perform different processing if (typeid (* Command) = typeid (BakeChickenWingCommand) {std: cout <"log: Waiter: Chicken Wings are missing. Please click another barbecue! "<
        
          Push_back (command); time_t now = time (0); std: cout <"log: Add order: command mode. kebab "<" time: "<: iterator it; for (it = orders-> begin (); it! = Orders-> end (); it ++) {(* it)-> ExcuteCommand () ;}}; # endif
        
       
      
     
    
   
  
 


 

 

(2) Client. cpp

 

# Include "Command. h" # include
 
  
# Include
  
   
Void main () {// preparations before opening shop Barbecuer * boy = new Barbecuer (); Command * bakeMuttonCommand1 = new BakeMuttonCommand (boy); Command * bakeMuttonCommand2 = new BakeMuttonCommand (boy ); command * bakeChickenWingCommand1 = new BakeChickenWingCommand (boy); Waiter * girl = new Waiter (); // opens the door for business, customer ordering girl-> SetOrder (bakeMuttonCommand1 ); girl-> SetOrder (bakeMuttonCommand2); girl-> SetOrder (bakeChickenWingCommand1); // after the order is completed, inform the kitchen girl-> Y (); delete boy, bakeMuttonCommand1, bakeMuttonCommand2, done; system ("pause ");}
  
 


 

(3) Run

Http://www.bkjia.com/kf/201412/364153.html previous

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.