desgn partten : 命令模式

 庆祝一下,第一次用工具画图,^_^

desgn partten : 命令模式_第1张图片

 

应用

  1. 可做任务队列的消费,将具体的操作封装成command,由receiver通用调用execute,感觉和线程池有点像啊。
  2. 日志系统的恢复操作,execute同时调用save,将操作持久化,当机器突然broke重启,顺序的load#command然后调用undo可以恢复现场。
  3. 其他
下面是head first的一个demo&所有demo类的截图
  • controller相当一个遥控器,控制着灯的开关,门的开关等
  • controller中通过一个堆栈实现命令的撤销操作
desgn partten : 命令模式_第2张图片
 
public class Gate {
    public void open() {
        System.out.println("gate open,,,");
    }
    public void close() {
        System.out.println("gate close,,,");
    }
}
 
public class Light {
    public void on() {
        System.out.println("light on,,,");
    }
    public void off() {
        System.out.println("light off,,,");
    }
}
 
public interface Command {
    void execute();
    void undo();
    void save();
    void load();
}
 
public class LightOnCommand implements Command {
    private Light light;
    public LightOnCommand(Light light) {
        this.light = light;
    }
    public void execute() {
        light.on();
    }
    public void undo() {
        light.off();
    }
    public void save() {
        // TODO
    }
    public void load() {
        // TODO
    }
}
 
public class GateOpenCommand implements Command {
    private Gate gate;
    public GateOpenCommand(Gate gate) {
        this.gate = gate;
    }
    public void execute() {
        gate.open();
    }
    public void undo() {
        gate.close();
    }
    public void save() {
        // TODO
    }
    public void load() {
        // TODO
    }
}
 
public class NoCommand implements Command {
    //所有方法没有实现,表示命令没有具体执行内容
    public void execute() {
        System.out.println("no command execute,,,");
    }
    public void undo() {
        System.out.println("no command undo,,,");
    }
    public void save() {}
    public void load() {}
}
 
public class Controller {
    private Command[] commands;
    private Stack<Command> undoStack;

    public Controller(int size) {
        this.commands = new Command[size];
        for (int i = 0; i < size; i++) {
            commands[i] = new NoCommand();
        }
        undoStack = new Stack<Command>();
    }

    public void setCommand(int index, Command command) {
        Preconditions.checkElementIndex(index, commands.length);
        Preconditions.checkNotNull(command);
        commands[index] = command;
    }

    public void undoOperation() {
        if (undoStack.isEmpty()) {
            System.out.println("undo stack empty");
            return;
        }
        undoStack.pop().undo();
    }

    public void onButtonPress(int index) {
        Preconditions.checkElementIndex(index, commands.length);
        commands[index].execute();

        undoStack.push(commands[index]);
    }
}
 
public class ClientTest {

    public static void main(String[] args) {
        Light light = new Light();
        Gate gate = new Gate();

        LightOnCommand lightOnCommand = new LightOnCommand(light);
        LightOffCommand lightOffCommand = new LightOffCommand(light);

        GateOpenCommand gateOpenCommand = new GateOpenCommand(gate);
        GateCloseCommand gateCloseCommand = new GateCloseCommand(gate);

        Controller controller = new Controller(6);

        controller.setCommand(0, lightOnCommand);
        controller.setCommand(1, lightOffCommand);
        controller.setCommand(2, gateOpenCommand);
        controller.setCommand(3, gateCloseCommand);

        controller.onButtonPress(0);
        controller.onButtonPress(1);
        controller.onButtonPress(2);
        controller.onButtonPress(3);
        controller.onButtonPress(4);
        controller.onButtonPress(5);

        System.out.println();

        controller.undoOperation();
        controller.undoOperation();
        controller.undoOperation();
        controller.undoOperation();
        controller.undoOperation();
        controller.undoOperation();
        controller.undoOperation();
        controller.undoOperation();
    }
}

 

下面是运行结果

light on,,,
light off,,,
gate open,,,
gate close,,,
no command execute,,,
no command execute,,,

no command undo,,,
no command undo,,,
gate open,,,
gate close,,,
light on,,,
light off,,,
undo stack empty
undo stack empty

 

你可能感兴趣的:(java,design,partten)