command mode

schema definition

Encapsulate "requests" as objects to parameterize other objects with different request queues or logs. Command mode also supports undoable operations

Pattern class diagram

Example

Different switches on the remote control switch examples of different electrical appliances, strong coupling design idea The remote control control class judges different switches and then calls different electrical appliances. This design is not conducive to later expansion and maintenance, and requires decoupling operations. When the switch is pressed, the remote control doesn't care what electrical appliance is bound to the switch, but if the switch is on, I will call the on operation of the bound electrical appliance. Therefore, the on, off, and cancellation of all electrical appliances are encapsulated in different commands, and the operation commands of different electrical appliances can be bound on the remote control as needed. When the switch is on or off, just call the different commands of the bound electrical appliance.

/**
 * 灯具厂商
 *
 * @author Colin
 * @create 2018-05-02
 **/
public class Light {

    private String name;

    public Light(String name){
        this.name=name;
    }

    public void on(){
        System.out.println(this.name+"开灯");
    }

    public void off(){
        System.out.println(this.name+"关灯");
    }
}



/**
 * 命令接口
 *
 * @author Colin
 * @create 2018-05-02
 **/
public interface Command {

    /**
     * 执行命令
     */
    public void execute();

    /**
     * 撤销
     */
    public void undo();

}


/**
 * 关灯命令
 *
 * @author Colin
 * @create 2018-05-02
 **/
public class LightOffCommand implements Command {

    private  Light light;

    public LightOffCommand(Light light){
        this.light=light;
    }
    @Override
    public void execute() {
        light.off();
    }

    @Override
    public void undo() {
        light.on();
    }
}

/**
 * 开灯命令
 *
 * @author Colin
 * @create 2018-05-02
 **/
public class LightOnCommand implements Command {
    private Light light;

    public LightOnCommand(Light light){
        this.light=light;
    }

    @Override
    public void execute() {
        light.on();
    }

    @Override
    public void undo() {
        light.off();
    }
}

/**
 * 空命令
 *
 * @author Colin
 * @create 2018-05-02
 **/
public class NoCommand implements Command {
    @Override
    public void execute() {

    }

    @Override
    public void undo() {

    }
}

The remote control, the caller Invoker

/**
 * 遥控器实现类
 *
 * @author Colin
 * @create 2018-05-02
 **/
public class RemoteControl {

    private Command[] onCommands;
    private Command[]  offCommands;
    private Command undoCommand;

    public RemoteControl(){
        onCommands=new Command[7];
        offCommands=new Command[7];
        Command noCommand=new NoCommand();
        for (int i=0;i<7;i++){
            onCommands[i]=noCommand;
            offCommands[i]=noCommand;
        }
        undoCommand=noCommand;
    }


    public void setCommand(int slot,Command onCommand,Command offCommand){
        onCommands[slot]=onCommand;
        offCommands[slot]=offCommand;
    }

    public void onButtonWasPushed(int slot){
        onCommands[slot].execute();
        undoCommand=onCommands[slot];
    }

    public void offButtonWasPushed(int slot){
        offCommands[slot].execute();
        undoCommand=offCommands[slot];
    }

    public void undoButtonWasPushed(){
        undoCommand.undo();
    }
}

Client, client, dynamic binding, decoupling


/**
 * 遥控器测试类
 *
 * @author Colin
 * @create 2018-05-02
 **/
public class RemoteLoader {

    @Test
    public void testRemoteControl(){
        Light livingLight=new Light("卧室");
        Light kitchenLight=new Light("厨房");

        LightOnCommand  livingRoomLightOnCommand=new LightOnCommand(livingLight);
        LightOffCommand livingRoomLightOffCommand=new LightOffCommand(livingLight);


        LightOnCommand  kitchenLightOnCommand=new LightOnCommand(kitchenLight);
        LightOffCommand kitchenLightOffCommand=new LightOffCommand(kitchenLight);


        RemoteControl remoteControl=new RemoteControl();
        remoteControl.setCommand(0,livingRoomLightOnCommand,livingRoomLightOffCommand);
        remoteControl.setCommand(1,kitchenLightOnCommand,kitchenLightOffCommand);


        remoteControl.onButtonWasPushed(0);
        remoteControl.offButtonWasPushed(0);

        remoteControl.onButtonWasPushed(1);
        remoteControl.offButtonWasPushed(1);

        remoteControl.undoButtonWasPushed();

    }
}

Summarize

  • The command pattern decouples the object that makes the request and the object that executes the request, that is, the above client (request object) and Light (execution request object)
  • The communication between the decoupled two is through the command object. The command object encapsulates one or a group of actions of the receiver, that is, the Commd object above, the light-on command and the light-off command.
  • The caller makes a request through the unified execute method of the command object, so that the action of the acceptor can be executed

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325234646&siteId=291194637