首页 > 代码库 > 设计模式之命令模式

设计模式之命令模式

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("\n------ Remote Control -------\n");        for (int i = 0; i < onCommands.length; i++) {            stringBuff.append("[slot " + i + "] " + onCommands[i].getClass().getName()                + "    " + offCommands[i].getClass().getName() + "\n");        }        stringBuff.append("[undo] " + undoCommand.getClass().getName() + "\n");        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();}

 

设计模式之命令模式