[Study Notes] Command of Design Mode

Source: Internet
Author: User

Written in the previous article Chain of Responsibility (Responsibility Chain) model notes, we learned a behavior-type design model. Today, let's continue with this topic to learn the Command mode. We can see that the responsibility chain mode is used to model the object (functional person) processing the request. The biggest difference between the Command mode is that it is used to model the request itself. This can be seen from its name. So it has the following modes: Action and Transaction. Old rules: first, we can intuitively understand what the command mode is. In our daily work, we often accept some "tasks" which are usually accompanied by specific requirements and correspond to specific executors. For example, I am responsible for one of the Feature projects. Then, any "command" proposed in the above header for any modification to this Feature will let me execute, because I have the necessary information to develop this Feature. The key to implementing this model lies in the abstract class of Command, which defines an interface for executing operations, such as the execute () operation. A specific Command subclass stores the receiver as an instance variable, implements the execute () operation, and specifies the receiver to act. We will explain the examples in more depth, so we can understand the basic points of this example: key points: sorting out target classification object behavior pattern scope criterion objects (this pattern processes the relationships between objects, which can be changed and more dynamic at runtime) the main function is to encapsulate a request as an object, so that you can parameterize the customer with different requests; queue requests or record request logs, and applicable situations that support undo operations when we need to abstract the actions to be executed to parameterize an object. Specify, arrange, and execute requests at different times. Cancellation is required. You need to support the modification log so that these modifications can be redone when the system crashes. Construct a system with high-level operations built on primitive operations. Part of the Command involved: Declares the ConcreteCommand interface for executing the operation: binds a receiver object to an action, and calls the corresponding operation of the receiver to implement executeClient: create a specific command object and set its Receiver Invoke: require the command to execute the request handler: Know how to perform operations related to executing a request. Any class can be used as a receiver to create a ConcreteCommand object and specify its Reciever object. an Invoker Object Storage ConcreteCommand object. The Invoker submits a request by calling the execute Operation of the Command object. If this command can be canceled, therefore, ConcreteCommand stores the current state before executing execute to cancel the ConcreteCommand object and execute this request to some operations of the caller who calls it. UML graphic example analysis-magic doll drive let's review the Witch Glinda's private customized magic doll (For details, refer to the Composite mode notes ). It uses a mysterious sensor as an Input device to receive commands issued by people and then execute them. For the sake of simplicity, we will give a specific example of "Wu Dou doll" today. This doll is like an empty-handed combat master. In order to show the combat techniques flexibly, it has a drive device corresponding to the arm and leg (ArmDrive & LegDrive ), people can give it commands such as Punch and FlyingKick. To enhance the scalability of the doll function, we certainly hope it can execute more and more commands in the future. The introduction of the Command design pattern helps us solve our needs well. It decouples the called object from the object that knows how to implement the operation. In our example, the sensor of the doll is the object that calls the operation, while the various drives are the objects that implement the specific operation, the Command mode makes the sensor do not need to know any driver information, this helps us to develop more drivers in the future. First, let's take a look at the most important Command class: Copy code 1 //... direction definitions... 2 # define NONE 0 3 # define D_SOUTH 1 4 # define D_NORTH 2 5 # define D_WEST 3 6 # define D_EAST 4 7 8 //... abstract Command class... 9 class Command {10 public: 11 virtual ~ Command (); 12 virtual void execute () = 0; 13 protected: 14 Command (); 15}; copy the code followed by PunchCommand, it causes the ArmDrive object to hit the target user. Note that the PunchCommand constructor requires an ArmDrive object as the parameter. AskDirection is a prompt for the user to tell the specific direction. Copy code 1 // Punch! 2 class PunchCommand: public Command {3 public: 4 PunchCommand (ArmDrive *); 5 6 virtual void execute (); 7 protected: 8 virtual int askDirection (); 9 private: 10 ArmDrive * _ drive; 11}; 12 13 PunchCommand: PunchCommand (ArmDrive * a) {14 _ drive = a; 15} 16 17 void PunchCommand: execute () {18 int direction = askDirection (); 19 20 if (direction) {21 _ drive-> punch (direction); 22} 23} copy the code. Similarly, we need a KickCommand The LegDrive object is used as its receiver. Here, askHeight is the height of the user to be kicked. If necessary, it must jump up. Copy code 1 // Kick! 2 class KickCommand: public Command {3 public: 4 KickCommand (LegDrive *); 5 6 virtual void execute (); 7 protected: 8 virtual int askHeight (); 9 virtual int askDirection (); 10 private: 11 LegDrive * _ drive; 12}; 13 14 KickCommand: PunchCommand (LegDrive * l) {15 _ drive = l; 16} 17 18 void KickCommand: execute () {19 int direction = askDirection (); 20 int height = askHeight (); 21 22 if (direction & height> 0) {23 _ drive-> kick (direction, height); 24} 25} copy the code, our sensors can trigger a doll by receiving and calling commands: Copy code 1 //... sensor Process... 2 ArmDrive * arm = ArmDrive: getInstance (); 3 LegDrive * leg = LegDrive: getInstance (); 4 Command * aCommand = new PunchCommand (arm ); 5 aCommand-> execute (); // execute command.6 delete aCommand; 7 aCommand = new KickCommand (leg); 8 aCommand-> execute (); // execute command. benefits of the Command mode for copying code This is not limited. You must be familiar with batch processing, macros, and other concepts. In combination with the Composite design mode (see index), we can design MacroCommand to execute a series of commands in batches. For MarcroCommand itself, it does not need to know the specific executors of its sub-commands, because it only needs to call sub-commands. It has the add and remove methods to manage sub-commands. Copy code 1 //... Macro Command... 2 class MacroCommand: public Command {3 public: 4 MacroCommand (); 5 virtual ~ MacroCommand (); 6 7 virtual void add (Command *); 8 virtual void remove (Command *); 9 10 virtual void execute (); 11 private: 12 List <Command *> * _ cmds; 13}; 14 15 void MacroCommand: execute () {16 ListIterator <Command *> I (_ cmds ); 17 18 for (I. first ();! I. isDone (); I. next () {19 Command * c = I. currentItem (); 20 c-> execute (); 21} 22} 23 24 void MacroCommand: add (Command * c) {25 _ cmds-> append (c ); 26} 27 28 void MacroCommand: remove (Command * c) {29 _ cmds-> remove (c); 30} copy the code for example, we want the doll to make a series of "tricks": copy the Code 1 //... punch Kick and Punch... 2 MacroCommand * aMCmd = new MacroCommand (); 3 aMCmd-> add (new PunchCommand (arm); 4 aMCmd-> add (new PunchCommand (arm )); 5 aMCmd-> add (new KickCommand (leg); 6 aMCmd-> add (new PunchCommand (arm); 7 aMCmd-> execute ();

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.