Command mode is a behavior mode.

Definition of command pattern

Command mode is a highly cohesive mode that encapsulates a request as an object, allowing you to parameterize clients with different requests, queue or log requests, and provide command undo and recovery capabilities. The core of command mode is the introduction of command class, through which the coupling degree of sender and receiver can be reduced. The request sender only needs to specify a command object, and then call the processing method of the request receiver through the command object.

Command mode

The command pattern can completely decouple the sender and receiver of the request. There is no direct reference relationship between the sender and receiver, and the object sending the request only needs to know how to send the request, but not how to complete the request.

Structure of the command pattern

The command mode contains the following roles:

  • Receiver Receiver role Command Receiver mode, where commands are transferred to perform operations.

  • Command The commands that the role needs to execute are declared here

  • The Invoker caller role receives and executes the command, which is the originator and caller of the command

Implementation of command pattern

Abstract the Command class.

public abstract class Command {  
    public abstract void execute(a);  
}
Copy the code

The caller Invoker class.

public class Invoker {  
    private Command command;  
      
    // Construct injection
    public Invoker(Command command) {  
        this.command = command;  
    }  
      
    // Set value injection
    public void setCommand(Command command) {  
        this.command = command;  
    }  
      
    // Business method, which calls the execute() method of the command class
    public void call(a) { command.execute(); }}Copy the code

For the Command class, set the corresponding receiver.

public  class ConcreteCommand extends Command {  
    private Receiver receiver; // Maintains a reference to the request receiver object
  
    public ConcreteCommand(Receiver receiver) {
        super(a);this.receiver = receiver;
    }

    public void execute(a) {  
        receiver.action(); // Call the request receiver's business handler action()}}Copy the code

Abstract receiver.

public  abstract class Receiver {  
    public abstract void action(a);
}  
Copy the code

The first recipient.

public class ConcreteReceiver extends Receiver{

    @Override
    public void action(a) {
        System.out.println("ConcreteReceiver receives the command!"); }}Copy the code

The second recipient.

public class ConcreteReceiver2 extends Receiver{

    @Override
    public void action(a) {
        System.out.println("ConcreteReceiver2 receives the command!"); }}Copy the code

Test class, create a connection receiver, respectively to send instructions.

public class Client {

    public static void main(String[] args) {
        Receiver receiver1 = new ConcreteReceiver();
        Command command1 = new ConcreteCommand(receiver1);

        Invoker invoker = new Invoker();
        invoker.setCommand(command1);
        invoker.call();

        Receiver receiver2 = new ConcreteReceiver2();
        Command command2 = newConcreteCommand(receiver2); invoker.setCommand(command2); invoker.call(); }}Copy the code

conclusion

The essence of command mode is to encapsulate the request. A request corresponds to a command, separating the responsibility of issuing the command from the responsibility of executing the command. Each command is an operation: the requesting party makes a request to perform an operation; The receiving party receives the request and performs the corresponding action. The command pattern allows the requesting party and the receiving party to be separated, so that the requesting party does not have to know the interface of the receiving party, let alone how the request is received, whether the operation is performed, when it is performed, and how it is executed. The key to the command pattern is the introduction of abstract command classes. The request sender programs against the abstract command classes, and only the concrete commands that implement the abstract command classes are associated with the request receiver.

The advantages of command mode are:

  • There is no dependency between the caller role and the receiver role. The caller only needs to call the Execute method of the Command abstract class to implement the function, without knowing which recipient is executing it.

  • The extensibility Command subclass can be extended very easily, while the Invoker caller and the high-level module Client do not generate serious code coupling

Its disadvantage is that it causes class inflation. If there are multiple subcommands corresponding to multiple Command subclasses, it is cumbersome.

Subscribe to the latest articles, welcome to follow my official account

reference

  1. Java design pattern command pattern
  2. Design mode Command Pattern –Command Pattern