Design pattern learning (15) -- [command pattern]

Gateway = = > Java design pattern of station B

❤❤❤ Thanks to Shang Silicon Valley ❤❤❤

Recently, I began to plan to learn design patterns. Come on!!!

Let's look at a case first

Bought a set of smart appliances, including lights, fans, refrigerators and washing machines. As long as you install an app on your mobile phone, you can control the work on these appliances. These smart appliances come from different manufacturers, but we don't want to install an app for each kind of home appliance and control them separately. We hope that only one app can control all smart appliances.

It looks like the appearance mode, but it is different. In the appearance mode, after adding a layer of interface, define several methods in the interface; You can directly control the process

To realize the need for one app to control all smart appliances, each smart appliance manufacturer must provide a unified interface
To call app, you need to introduce command mode

Command mode

Using command mode encapsulates a request as an object so that different parameters can be used to represent different requests (i.e. naming), and command mode also supports revocable operations.
Command mode decouples the action requester from the action performer object

In the case of home appliances just now;
Action requester = = > mobile app, action executor = = > one home appliance of each manufacturer

Generally speaking; Command mode = = >

For example, a "general" wants to issue orders for "soldiers" to fight;
Well, the soldier accepted the order; But the "general" did not know which soldier had received the order to fight;
In this case; "General" is the issuer of the order; "Soldier" is the executor of the order;

If we put it here in the traditional way, it may be that the "general" issued an order: Zhang San and Li Si, you go to fight!

Schematic diagram of command mode:

Command: ⇒ (command role), interface or abstract class; Declare all commands that need to be executed

Concretecommand = > bind the receiver object and the corresponding action, and call the corresponding operation of the receiver

The decoupling between "request initiator" and "request executor" is realized through command object, which acts as a bridge.
Disadvantages: some systems may have too many specific command classes, increasing the complexity of the system

Cases of household appliances

Empty command used here; Used to initialize each button; Or it can be understood as "reset operation"

Command publisher abstract interface command;

//Command:
public interface Command {
    //Perform operations;
    void execute();
    //Undo operation;
    void undo();
}

Command recipient LightReceiver

//Order receiver; electric light;
public class LightReceiver {
    public void on(){
        System.out.println("electric light--->open!!!");
    }

    public void off(){
        System.out.println("electric light--->shut!!!");
    }
}

LightOnCommand

//Light on command;
public class LightOnCommand implements Command{

    //Polymerization;
    LightReceiver lightReceiver;

    public LightOnCommand(LightReceiver lightReceiver) {
        this.lightReceiver = lightReceiver;
    }

    //Execution;
    public void execute() {
        lightReceiver.on();
    }

    //revoke;
    public void undo() {
        lightReceiver.off();
    }
}

LightOffCommand

//Light off Commander
public class LightOffCommand implements Command {

    //Polymerization;
    LightReceiver lightReceiver;

    public LightOffCommand(LightReceiver lightReceiver) {
        this.lightReceiver = lightReceiver;
    }

    //Execution;
    public void execute() {
        lightReceiver.off();
    }

    //revoke;
    public void undo() {
        lightReceiver.on();
    }
}

Null command

//Empty command; Used to initialize the button to be operated;
public class NullCommand implements Command{

    public void execute() {

    }

    public void undo() {

    }
}

Remote control RemoteController

//Analog remote control;
public class RemoteController {
    //Click on; Button command group;
    Command[] onCommands;
    //Click close; Button command group;
    Command[] offCommands;

    //Revoke the order;
    Command undoCommand;

    //initialization;
    public RemoteController() {
        onCommands = new Command[5];
        offCommands = new Command[5];
        for (int i = 0; i < 5; i++) {
            onCommands[i] = new NullCommand();
            offCommands[i] = new NullCommand();
        }
    }
    //Set the command for the button;
    public void setCommands(int num,Command onCommand,Command offCommand){
        onCommands[num] = onCommand;
        offCommands[num] = offCommand;
    }

    //Button for selecting which electrical appliance to turn on;
    public void openWhat(int num){
        onCommands[num].execute();
        //Prepare for revocation of orders;
        undoCommand=onCommands[num];
    }

    //The button to select which appliance to turn off;
    public void closeWhat(int num){
        offCommands[num].execute();
        undoCommand=offCommands[num];
    }

    //Click cancel;
    public void undoMethod(){
        undoCommand.undo();
    }
}

Simulation client

public class Client {
    public static void main(String[] args) {
        //Remote control;
        RemoteController remoteController = new RemoteController();
        //Light -- > command receiver;
        LightReceiver lightReceiver =new LightReceiver();

        //Open command; Off command;
        LightOnCommand lightOnCommand = new LightOnCommand(lightReceiver);
        LightOffCommand lightOffCommand = new LightOffCommand(lightReceiver);

        //Select the switch of the fourth electric lamp;
        remoteController.setCommands(4,lightOnCommand,lightOffCommand);

        //The light is on;
        remoteController.openWhat(4);

        //Lights off;
        remoteController.closeWhat(4);
        System.out.println("=======revoke=======");
        //Undo operation!!!;
        remoteController.undoMethod();
    }
}

Execution;

electric light--->open!!!
electric light--->shut!!!
=======revoke=======
electric light--->open!!!

The command pattern appears in the use of Spring framework JdbcTemplate

Type into the JdbcTemplate class;

Find the query method

Type its return value and call the method query; Found a defined inner class QueryStatementCallback;

This inner class has an implemented interface statementcallback < T >; He has a defined abstract method doInStatement;

Then the interface statementcallback < T > here can be regarded as a top-level command interface;
The implementation class QueryStatementCallback is the command implementation class; It is also the receiver of the command;
The JdbcTemplate class is the command caller

This method doInStatement; Implemented by four classes

Tags: Design Pattern

Posted on Sat, 02 Oct 2021 14:31:39 -0400 by awpti