14.命令模式

我们经常需要对一些对象发送命令请求,但是我们并不知道是哪个具体的对象接受到了这些命令,这个时候可以使用命令模式

命令模式会将一个请求封装为一个对象,来使用不同的参数表达不同的请求,同时命令应该是可以撤销的

对于这样一个遥控器,使用命令模式去实现它的功能

 

 

 接口类和几个实现类

/**
 * @author wuyimin
 * @create 2021-07-28 11:08
 * @description
 */
public interface Command {
    //执行
    public void execute();
    //撤销
    public void undo();
}

/**
 * @author wuyimin
 * @create 2021-07-28 11:18
 * @description 空操作,即初始化操作
 */
public class NoCommand implements Command{
    @Override
    public void execute() {

    }

    @Override
    public void undo() {

    }
}

/**
 * @author wuyimin
 * @create 2021-07-28 11:10
 * @description 关闭电灯的命令 聚合了灯的行为类
 */
public class LightOffCommand implements Command {
    LightReceiver lightReceiver;

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

    @Override
    public void execute() {
        System.out.print("执行了操作---");
        lightReceiver.off();
    }
    @Override
    public void undo() {
        System.out.print("撤销了刚刚的操作---");
        lightReceiver.on();

    }
}

/**
 * @author wuyimin
 * @create 2021-07-28 11:09
 * @description 打开电灯的命令 聚合了灯的行为类
 */
public class LightOnCommand implements Command {
    LightReceiver lightReceiver;

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

    @Override
    public void execute() {
        System.out.print("执行了操作---");
        lightReceiver.on();
    }
    @Override
    public void undo() {
        System.out.print("撤销了刚刚的操作---");
        lightReceiver.off();

    }
}

灯的行为类

/**
 * @author wuyimin
 * @create 2021-07-28 11:11
 * @description 灯的具体行为
 */
public class LightReceiver {
    public void on(){
        System.out.println("电灯打开了");
    }

    public void off(){
        System.out.println("电灯关闭了");
    }

}

遥控器类和方法入口

public class RemoteController {
    //开的按钮
    private Command[] onCommands;
    //关的按钮
    private Command[] offCommands;
    //撤销的按钮
    private Command undoCommand;

    public RemoteController() {
        //初始化,必须先申请空间再new出对象
        this.onCommands =new Command[5];
        this.offCommands =new Command[5];
        for (int i = 0; i < 5; i++) {
            onCommands[i]=new NoCommand();
            offCommands[i]=new NoCommand();
        }
    }
    //设置按钮的属性
    public void setButtonProperties(int i,Command on,Command off){
        onCommands[i]=on;
        offCommands[i]=off;
    }
    //设置打开按钮按下事件
    public void onButtonPushed(int num){
       onCommands[num].execute();
       //记录上次按下的按钮
       undoCommand=onCommands[num];
    }

    //设置关闭按钮按下事件
    public void offButtonPushed(int num){
        offCommands[num].execute();
        //记录上次按下的按钮
        undoCommand=offCommands[num];
    }
    //设置撤销按钮按下事件
    public void undoButtonPushed(){
        undoCommand.undo();
    }

    public static void main(String[] args) {
        RemoteController remoteController = new RemoteController();
        //设置按钮的属性
        remoteController.setButtonProperties(0,new LightOnCommand(new LightReceiver()),new LightOffCommand(new LightReceiver()));
        remoteController.onButtonPushed(0);
        remoteController.offButtonPushed(0);
        remoteController.undoButtonPushed();
    }
}

 

posted @ 2021-07-28 11:43  一拳超人的逆袭  阅读(32)  评论(0编辑  收藏  举报