13. Command Mode

Command Mode

First, application examples

1) a set of intelligent appliances, illumination lights, fans, refrigerators, washing machines

2) these appliances from different manufacturers, we do not want appliances are installed for each a app, hope can be controlled through a whole app

3) To achieve a app to control all appliances, then each appliance manufacturers to provide a unified interface to the app calls. Then you can consider using the command mode.

4) The command template "operation requester" decoupling from "doer of the action" object

5) instances, the action requester is a mobile phone app, every performer manufacturers of home appliances

Second, the command mode

1. Basic Introduction

  • Command mode makes the request sender and recipient request to eliminate the coupling between each other , so that calls between objects more flexible and decoupled.
  • Plain understanding: General issued an order to the soldiers to do it.

1) Invoker: the role of the caller (General)

2) Receiver: receiver role, know how to implement and execute a related operations (specific executor command soldiers request)

3) Command: a command role, all commands to be executed are here, it can be an interface or abstract class (connected generals and soldiers)

4) ConcreteCommand: a receiver object to a binding action, the recipient calls the corresponding operation achieved execute

Third, the command mode to solve the problem of smart appliances

1. Analysis of ideas

2. code implementation

//抽象命令接口
public interface Command {
    //执行动作(操作)
    public void execute();
    //撤销动作(操作)
    public void undo();
}
// 具体命令
public class LightOffCommand implements Command {
    // 聚合LightReceiver
    LightReceiver light;

    // 构造器
    public LightOffCommand(LightReceiver light) {
            super();
            this.light = light;
        }

    @Override
    public void execute() {
        // 调用接收者的方法
        light.off();
    }

    @Override
    public void undo() {
        // 调用接收者的方法
        light.on();
    }
}

public class LightOnCommand implements Command {
    //聚合LightReceiver
    LightReceiver light;
    
    //构造器
    public LightOnCommand(LightReceiver light) {
        super();
        this.light = light;
    }
    
    @Override
    public void execute() {
        //调用接收者的方法
        light.on();
    }

    @Override
    public void undo() {
        //调用接收者的方法
        light.off();
    }
}
// 接收者
public class LightReceiver {
    public void on() {
        System.out.println(" 电灯打开了.. ");
    }
    
    public void off() {
        System.out.println(" 电灯关闭了.. ");
    }
}
//遥控(命令者)
public class RemoteController {
    // 开 按钮的命令数组
    Command[] onCommands;
    Command[] offCommands;

    // 执行撤销的命令
    Command undoCommand;

    // 构造器,完成对按钮初始化
    public RemoteController() {
        onCommands = new Command[5];
        offCommands = new Command[5];
        for (int i = 0; i < 5; i++) {
            onCommands[i] = new NoCommand();
            offCommands[i] = new NoCommand();
        }
    }

    // 给我们的按钮设置你需要的命令
    public void setCommand(int no, Command onCommand, Command offCommand) {
        onCommands[no] = onCommand;
        offCommands[no] = offCommand;
    }

    // 按下开按钮
    public void onButtonWasPushed(int no) { // no 0
        // 找到你按下的开的按钮, 并调用对应方法
        onCommands[no].execute();
        // 记录这次的操作,用于撤销
        undoCommand = onCommands[no];

    }

    // 按下关按钮
    public void offButtonWasPushed(int no) { // no 0
        // 找到你按下的关的按钮, 并调用对应方法
        offCommands[no].execute();
        // 记录这次的操作,用于撤销
        undoCommand = offCommands[no];

    }
    
    // 按下撤销按钮
    public void undoButtonWasPushed() {
        undoCommand.undo();
    }
}
public class Client {
    public static void main(String[] args) {
        //使用命令设计模式,完成通过遥控器,对电灯的操作
        
        //创建电灯的对象(接受者)
        LightReceiver lightReceiver = new LightReceiver();
        
        //创建电灯相关的开关命令
        LightOnCommand lightOnCommand = new LightOnCommand(lightReceiver);
        LightOffCommand lightOffCommand = new LightOffCommand(lightReceiver);
        
        //需要一个遥控器
        RemoteController remoteController = new RemoteController();
        
        //给我们的遥控器设置命令, 比如 no = 0 是电灯的开和关的操作
        remoteController.setCommand(0, lightOnCommand, lightOffCommand);
        System.out.println("--------按下灯的开按钮-----------");
        remoteController.onButtonWasPushed(0);
        System.out.println("--------按下灯的关按钮-----------");
        remoteController.offButtonWasPushed(0);
        System.out.println("--------按下撤销按钮-----------");
        remoteController.undoButtonWasPushed();
    }
}

Fourth, the command mode Notes

1) The object is decoupled from the initiation request execution object. The caller, the caller simply call the command object when the object request initiated by the execute () method can allow the recipient to work, without having to know who the specific recipients Yes.

2) easy to design a command queue, as long as the Command object into the queue, you can multi-threaded execution command

3) easy to implement a request for revocation and redo

4) command inadequate mode : the system may cause excessive concrete command class, increases the complexity of the system.

5) Air Command is a design pattern, eliminating the need for us to judge op.

6) Command Mode classic scenario: a button interface is a command mode CMD (DOS command) orders undo / redo, triggering a feedback mechanism

Guess you like

Origin www.cnblogs.com/chao-zjj/p/11333783.html