Definition of command pattern

Definition: Encapsulates a request as an object that allows you to parameterize clients with different requests, queue requests or log requests, and provide command undo and recovery capabilities

In layman’s terms, when there are different requests, each request is encapsulated as an object, and different requests call different performers to execute them

The general class diagram for command mode is as follows:

 

Its parts are as follows:

  1. Receiver Receiver. This is the role that does the work, where commands are delivered and each Receiver corresponds to a task
  2. Command Indicates the role of a Command. All commands that need to be executed are declared here, and command roles execute commands by calling Receiver
  3. Invoker caller. The command is received and executed.

Receiver code:

 

Why is Receiver an abstract class? Because there can be more than one receiver, having more than one requires defining an abstract set of all the features.

Command role is the core of command mode, and its abstract command class code is as follows:

 

Based on the requirements of the environment, there can be multiple command classes. The code is as follows:

 

In each command class, the constructor defines the recipient for which the command is issued and the subject for which a command is received.

The caller is very simple, only to achieve the command transfer, the code is as follows;

 

So, how do you call it? The scenario class code is as follows:

 

At this point, a simple command pattern is complete

Application of command mode

Advantages of command mode

  1. Decoupling between classes: There is no dependency between the caller and the receiver. The caller only needs to call the Execute method of the Command abstract class to implement the function, without knowing which recipient executes it.
  2. Extensibility. Subclasses of Command can be easily extended, while the Invoker caller and the higher-level module Client do not generate serious code coupling
  3. Command mode is better combined with other modes. The command mode can be combined with the responsibility chain mode to realize the command family parsing task. Combined with the template method pattern, the bloat problem of the Command subclass can be reduced

Disadvantages of command mode

Subclasses of Command, if you have N commands, you’re going to have a problem, you’re going to have N subclasses of Command, and it’s very bloated

Application scenarios of the command mode

Command mode can be used wherever it is considered to be a command. For example, in GUI development, a button click is a command and command mode can be used. When emulating DOS commands, of course, you should also use command mode


In the above code, the Receiver is exposed to the Client, which is not good. The command mode Receiver is generally encapsulated in the actual application. Each command is the encapsulation of one or more receivers. We can deal with the coupling relationship between command role and receiver role through meaningful class name or command name in the project, reduce the dependency of high-level module on low-level module, and improve the overall stability of the system

The modified code is as follows:

 

 

The receiver encapsulation is realized, and each command completes a single responsibility, instead of completing different responsibilities according to the receiver, and the receiver is not considered when the high-level module is called. The scenario class code is as follows:

 

High level module directly issue command, perfect!!