Design mode-Command mode

Source: Internet
Author: User

definition: encapsulates a request as an object, allowing you to parameterize the client with different requests, queue requests, or log request logs, which can provide undo and redo functions for the command.

Type: behavior class mode

Class Diagram:

Structure of the command pattern

As the name implies, the command pattern is the encapsulation of the command, first of all look at the basic structure of the command pattern class diagram:

    • Command class: is an abstract class that declares commands that need to be executed, and generally publishes an Execute method to execute the command.
    • Concretecommand class: The implementation class of the command class, which implements the methods declared in the abstract class.
    • Client class: the final caller class.

The role of the above three classes should be better understood, the following we focus on the Invoker class and Recevier class.

    • Invoker class: caller, responsible for invoking the command.
    • Receiver class: receiver, responsible for receiving commands and executing commands.

The so-called encapsulation of the command, plainly, is simply to write a series of operations into a method, and then for the client to call on the line, reflected on the class diagram, only need a Concretecommand class and the client class can complete the package of the command, even further, in order to increase flexibility, It is possible to add a command class to the appropriate abstraction, what is the role of this caller and receiver?

In fact, we can change an angle to think: if it is simply to encapsulate some operations as a command for others to call, how can be called a pattern? As a behavior class pattern, the command pattern must first be low-coupling, with low coupling to increase flexibility, and the purpose of adding two roles for callers and receivers is to do so. The general code for the command pattern is as follows:

Class Invoker {private command command;      public void SetCommand (Command command) {this.command = command;      } public void Action () {This.command.execute ();  }} abstract class Command {public abstract void execute ();      } class Concretecommand extends Command {private receiver receiver;      Public Concretecommand (receiver receiver) {this.receiver = receiver;      } public void Execute () {this.receiver.doSomething ();      }} class Receiver {public void dosomething () {System.out.println ("Recipient-business logic processing");          }} public class Client {public static void main (string[] args) {receiver receiver = new receiver ();          Command command = new Concretecommand (receiver);            The client executes the specific command method directly (this way matches the class diagram) Command.Execute ();          The client executes the command through the caller Invoker Invoker = new Invoker ();          Invoker.setcommand (command);      Invoker.action ();   }  }

Through the code we can see that when we invoke it, the timing of execution is first the caller class, then the command class, and finally the receiver class. In other words, the execution of a command is divided into three steps, which is much lower in coupling than encapsulating all operations into a class, and this is precisely the essence of the command pattern: separating the caller of the command from the performer, so that both parties do not have to care about how the other party operates.

Advantages and Disadvantages of command mode

First, the command pattern is well encapsulated: Each command is encapsulated, and for the client, it is necessary to invoke the corresponding command without knowing how the command is executed. For example, there is a set of commands for file operations: Create a new file, copy a file, delete a file. If these three operations are encapsulated into a command class, the client only needs to know that there are three command classes, as for the logic encapsulated in the command class, the client does not need to know.

Second, the command pattern is very extensible, in the command mode, in the receiver class is generally the most basic encapsulation of operations, the command class through the basic operation of the two encapsulation, when the addition of new commands, the command class is generally not written from zero, there are a large number of receiver classes to call, There are also a number of command classes to invoke, and code reusability is good. For example, in the operation of the file, we need to add a cut file command, then only need to copy the file and delete the file of the two commands together, it is very convenient.

Finally, the disadvantage of the command mode, that is, if a lot of command, development will be a headache. In particular, a lot of simple commands, the implementation of a few lines of code, and the use of command mode, without the control of the command is much simpler, you need to write a command class to encapsulate.

Application scenarios for the command pattern

For most of the function of the request-response mode, it is more suitable to use the command mode, as the command mode definition says, the command mode is convenient for realizing the functions of logging and revocation operation.

Summarize

It is a very tangled issue for all developers to use no-mode for an occasion. Sometimes, because of the anticipation of some changes in demand, for the flexibility and scalability of the system to use a design pattern, but the anticipated demand is not, on the contrary, not foreseen the demand is a lot, resulting in the change in the code, the use of the design pattern instead of the opposite effect, So that the whole project team complained. Such an example, I believe every programmer has encountered. Therefore, based on the principle of agile development, when we design the program, if according to the current needs, do not use a certain mode can be well solved, then we do not introduce it, because to introduce a design pattern is not difficult, we can really need to use the system once again, introduce this design pattern.

Take command mode, we are developing, the function of request-response mode is very common, in general, we will encapsulate the response action of the request into a method, this encapsulation method can be called command, but not command mode. Whether or not to raise this design to the height of the pattern will be considered separately, because, if the use of command mode, you should introduce the caller, receiver two characters, originally placed in a place of logic scattered in three classes, design, you must consider the cost is worth.

Design mode-Command mode

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.