zoukankan      html  css  js  c++  java
  • 设计模式之命令模式

    public class RemoteLoader {
    
        public static void main(String[] args) {
    
            RemoteControl remoteControl = new RemoteControl();
    
            Light light = new Light("Living Room");
            TV tv = new TV("Living Room");
            Stereo stereo = new Stereo("Living Room");
            Hottub hottub = new Hottub();
     
            LightOnCommand lightOn = new LightOnCommand(light);
            StereoOnCommand stereoOn = new StereoOnCommand(stereo);
            TVOnCommand tvOn = new TVOnCommand(tv);
            HottubOnCommand hottubOn = new HottubOnCommand(hottub);
            LightOffCommand lightOff = new LightOffCommand(light);
            StereoOffCommand stereoOff = new StereoOffCommand(stereo);
            TVOffCommand tvOff = new TVOffCommand(tv);
            HottubOffCommand hottubOff = new HottubOffCommand(hottub);
    
            Command[] partyOn = { lightOn, stereoOn, tvOn, hottubOn};
            Command[] partyOff = { lightOff, stereoOff, tvOff, hottubOff};
      
            MacroCommand partyOnMacro = new MacroCommand(partyOn);
            MacroCommand partyOffMacro = new MacroCommand(partyOff);
     
            remoteControl.setCommand(0, partyOnMacro, partyOffMacro);
      
            System.out.println(remoteControl);
            System.out.println("--- Pushing Macro On---");
            remoteControl.onButtonWasPushed(0);
            System.out.println("--- Pushing Macro Off---");
            remoteControl.offButtonWasPushed(0);
        }
    }

    RemoteControl.java

    public class RemoteControl {
        Command[] onCommands;
        Command[] offCommands;
        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();
        }
     
        public String toString() {
            StringBuffer stringBuff = new StringBuffer();
            stringBuff.append("
    ------ Remote Control -------
    ");
            for (int i = 0; i < onCommands.length; i++) {
                stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName()
                    + "    " + offCommands[i].getClass().getName() + "
    ");
            }
            stringBuff.append("[undo] " + undoCommand.getClass().getName() + "
    ");
            return stringBuff.toString();
        }
    }

    LightOnCommand.java

    public class LightOnCommand implements Command {
        Light light;
    
        public LightOnCommand(Light light) {
            this.light = light;
        }
    
        public void execute() {
            light.on();
        }
    
        public void undo() {
            light.off();
        }
    }

    Command.java

    public interface Command {
        public void execute();
        public void undo();
    }
  • 相关阅读:
    zstack(一)运行及开发环境搭建及说明(转载)
    ZStack深度试用:部署、架构与网络及其与OpenStack的对比
    saltops 安装及相关环境安装
    windows下安装rabbitmq的步骤详解
    SQLServer就更新并发处理的解决方案及测试!
    mysql 8.0.20安装教程,mysql详细安装教程
    .NET中的简单的并行
    四、VSCode调试vue项目
    三、使用VSCode配置简单的vue项目
    二、windows下搭建vue开发环境+IIS部署
  • 原文地址:https://www.cnblogs.com/z888/p/5971302.html
Copyright © 2011-2022 走看看