JAVA設計模式之 13.命令設計模式

bulingbuling^_^發表於2020-09-30

 命令設計模式:

  1. 將發起請求的物件與執行請求的物件解耦。發起請求的物件是呼叫者,呼叫者只要呼叫命令物件的 execute()方法就可以讓接收者工作,而不必知道具體的接收者物件是誰、是如何實現的,命令物件會負責讓接收者執行請求的動作,也就是說:”請求發起者”和“請求執行者”之間的解耦是通過命令物件實現的,命令物件起到了紐帶橋樑的作用。
  2. 容易設計一個命令佇列。只要把命令物件放到列隊,就可以多執行緒的執行命令
  3. 容易實現對請求的撤銷和重做
//命令設計模式
public class Command {
    public static void main (String[] args) {

        //接受者
        LightReceiver lightReceiver = new LightReceiver();

        //命令
        LightOnCommand lightOnCommand = new LightOnCommand(lightReceiver);
        LightOffCommand lightOffCommand = new LightOffCommand(lightReceiver);

        //控制
        RemoteController remoteController = new RemoteController();

        //處理
        remoteController.setCommand(0, lightOnCommand, lightOffCommand);
        System.out.println("*************開燈******************");
        remoteController.onButtonWasPushed(0);
        System.out.println("*************關燈****************");
        remoteController.offButtonWasPushed(0);
        System.out.println("*************撤銷****************");
        remoteController.undoButtonWasPushed();
    }
}

interface CommandInterface {
    void execute();
    void undo();
}

class LightOnCommand implements CommandInterface {

    private LightReceiver lightReceiver;

    //通過構造器傳入lightReceiver
    public LightOnCommand(LightReceiver lightReceiver) {
        this.lightReceiver = lightReceiver;
    }

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

    @Override
    public void undo() {
        lightReceiver.off();
    }
}
class LightOffCommand implements CommandInterface {

    private LightReceiver lightReceiver;

    //通過構造器傳入lightReceiver
    public LightOffCommand(LightReceiver lightReceiver) {
        this.lightReceiver = lightReceiver;
    }

    @Override
    public void execute() {
        lightReceiver.off();
    }

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

//空命令,用於初始化
class NoCommand implements CommandInterface {

    @Override
    public void execute() {

    }

    @Override
    public void undo() {

    }
}

class LightReceiver {
    public void on(){
            System.out.println("電燈開啟了");
    }

    public void off(){
        System.out.println("電燈關閉了");
    }
}

class RemoteController {
    CommandInterface[] onCommands;
    CommandInterface[] offCommands;
    CommandInterface undoCommand;

    //構造器 完成初始化

    public RemoteController() {
        onCommands = new CommandInterface[5];
        offCommands = new CommandInterface[5];

        for (int i = 0; i < 5; i++) {
            onCommands[i] = new NoCommand();
            offCommands[i] = new NoCommand();
        }
    }

    public  void setCommand(int no,  CommandInterface onCommand, CommandInterface offCommand) {
        onCommands[no] = onCommand;
        offCommands[no] = offCommand;

    }

    public void onButtonWasPushed(int no) {
        onCommands[no].execute();

        undoCommand = onCommands[no];
    }

    public void offButtonWasPushed(int no) {
        offCommands[no].execute();

        undoCommand = offCommands[no];
    }

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

 

相關文章