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;

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!!!");


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

    LightReceiver lightReceiver;

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

    public void execute() {

    public void undo() {;


//Light off Commander
public class LightOffCommand implements Command {

    LightReceiver lightReceiver;

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

    public void execute() {;

    public void undo() {

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;

    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){
        //Prepare for revocation of orders;

    //The button to select which appliance to turn off;
    public void closeWhat(int num){

    //Click cancel;
    public void undoMethod(){

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;

        //The light is on;

        //Lights off;
        //Undo operation!!!;


electric light--->open!!!
electric light--->shut!!!
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