命令模式

基本介绍

(1)命令模式:在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道请求的程序是哪个,我们只需在程序运行时指定具体的请求接收者即可,此时,可以使用命令模式来进行设计

(2)命名模式使得请求发送者与请求接收者消除彼此之间的耦合,让对象之间的调用关系更加灵活,实现解耦

(3)在命令模式中,会将一个请求封装为一个对象,以便使用不同参数来表示不同的请求,同时命令模式也支持可撤销的操作

 

代码实现

//命令模式
public class CommandMode {
    public static void main(String[] args) {
        InstructionSet instructionSet=new InstructionSet();
        Command onCommand = new OnCommand(instructionSet);
       Command offCommand = new OffCommand(instructionSet);
        Controller controller = new Controller();
        //设置命令
        controller.set(0,onCommand,offCommand);
        //开启
        controller.onButton(0);
        //关闭
        controller.offButton(0);
        System.out.println("撤销操作,重新开启!!!!!!!!!");
        controller.undoButton();
    }
}

interface Command {

    //执行动作(操作)
    public void execute();

    //撤销动作(操作)
    public void undo();
}
//执行者
class OnCommand implements Command {
    private InstructionSet instructionSet;

    public OnCommand(InstructionSet instructionSet) {
        this.instructionSet = instructionSet;
    }


    public void execute() {
        instructionSet.on();
    }

    public void undo() {
        instructionSet.off();
    }
}
//执行者
class OffCommand implements Command {
    private InstructionSet instructionSet;

    public OffCommand(InstructionSet instructionSet) {
        this.instructionSet = instructionSet;
    }


    public void execute() {
        instructionSet.off();
    }

    public void undo() {
        instructionSet.on();
    }
}

/**
 * 没有任何命令,即空执行: 用于初始化每个按钮, 当调用空命令时,对象什么都不做
 * 其实,这样是一种设计模式, 可以省掉对空判断
 */
class NoCommand implements Command {


    public void execute() {
    }

    public void undo() {
    }

}

class InstructionSet {
    public void on() {
        System.out.println("摄像头录制功能开启~~~");
    }

    public void off() {
        System.out.println("摄像头录制功能关闭~~~~");
    }
}
//请求发起者
class Controller {
    Command[] onCommands;
    Command[] offCommands;
    Command undoCommand;

    public Controller() {
        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 set(int o, Command on, Command off) {
        onCommands[o] = on;
        offCommands[o] = off;
    }

    //开启方法
    public void onButton(int o) {
        onCommands[o].execute();
        undoCommand = onCommands[o];
    }

    //关闭方法
    public void offButton(int o) {
        offCommands[o].execute();
        undoCommand = offCommands[o];
    }

    //撤销方法
    public void undoButton() {
        undoCommand.undo();
    }

}

 

 

注意事项和细节

(1)将发起请求的对象与执行请求的对象解耦。发起请求的对象是调用者,调用者只要调用命令对象的execute()方法就可以让接收者工作,而不必知道具体的接收者对象是谁,是如何实现的,命令模式会负责让接收者执行请求的动作,也就是是:“请求发起者”和“请求执行者”之间的解耦是通过命令对象实现的,命令对象起到了纽带桥梁的作用

(2)容易设计一个命令队列。只要把命令对象放到队列,就可以多线程的执行命令

(3)容易实现对请求的撤销和重做

(4)命令模式不足:可能导致某些系统有过多的具体命令类,增加了系统的复杂度,这点在使用的时候需注意

(5)空命令也是一种设计模式,他为我们省去了判空的操作

(6)命令模式经典的应用场景:界面的一个按钮都是一个命令,模拟CMD(DOS命令)订单的撤销/恢复,触发-反馈机制

posted @ 2021-08-28 22:59  袁志航  阅读(114)  评论(0编辑  收藏  举报