。命令模式将发出请求的对象和执行请求的对象解耦。
2。在被解耦的两者之间是通过命令对象进行沟通的。命令对象封装了接受者和一个或者一组动作。
3。调用者通过调用命令对象的execute()发出请求,这会使得接受者的动作被调用。
4 。调用者可以接受命令当作参数,甚至在运行时动态进行。
5。命令可以支持撤销,做法是实现一个undo()方法来回到execute()被执行前的状态。
6。宏命令是命令的一种简单的延伸,允许调用多个命令。宏方法也可以支持撤销。
7。实际操作时,很常见使用“聪明”命令对象,也就是直接实现了请求,而不是将工作委托给接收者。
8。命令也可以用来实现日志和事物系统。
package pattern; public interface Command { public void execute(); public void undo(); } class Light{ public void on(){ System.out.println("Light on"); } public void off(){ System.out.println("Light off"); } public Light(String ms){ System.out.println(ms); } public Light(){ } } class TV { public void on(){ System.out.println("TV on"); } public void off(){ System.out.println("TV off"); } public TV(String ms){ System.out.println(ms); } public TV(){ } } class Hottub{ public void on(){ System.out.println("Hottub on"); } public void off(){ System.out.println("Hottub off"); } public Hottub(String ms){ System.out.println(ms); } public Hottub(){ } } class CeilingFan{ public static final int HIGH=3; public static final int MEDIUM=2; public static final int LOW=1; public static final int OFF=0; String location; int speed; public void on(){ } public void off(){ speed=OFF; } public CeilingFan(String ms){ this.location=location; speed=OFF; System.out.println(ms); } public void high(){ speed=HIGH; } public void medium(){ speed=MEDIUM; } public void low(){ speed=LOW; } public int getSpeed(){ return speed; } public CeilingFan(){ } } class GarageDoor{ public void up(){ System.out.println("GarageDoor up "); } public void down(){ System.out.println("GarageDoor down"); } public void stop(){ System.out.println("GarageDoor stop"); } public void lightOn(){ System.out.println("GarageDoor lightOn"); } public void lightOff(){ System.out.println("GarageDoor LightOff"); } public GarageDoor(){} public GarageDoor(String ms){ System.out.println(ms); } } class Stereo{ public Stereo(){} public Stereo(String ms){ System.out.println(ms); } int volume=0; public void on(){ System.out.println("Stereo on"); } public void off(){ System.out.println("Stereo off"); } public void setCD(){ System.out.println("Stereo CD"); } public void setVolume(int vol){ volume= vol; } } class LightOnCommand implements Command{ Light light; public LightOnCommand(Light light){ this.light=light; } public void execute(){ light.on(); } public void undo(){ light.off(); } } class TVOnCommand implements Command{ TV tv; public TVOnCommand(TV tv){ this.tv=tv; } public void execute(){ tv.on(); } public void undo(){ tv.off(); } } class TVOffCommand implements Command{ TV tv; public TVOffCommand(TV tv){ this.tv=tv; } public void execute(){ tv.off(); } public void undo(){ tv.on(); } } class HottubOnCommand implements Command{ Hottub hottub; public HottubOnCommand(Hottub hottub){ this.hottub=hottub; } public void execute(){ hottub.on(); } public void undo(){ hottub.off(); } } class HottubOffCommand implements Command{ Hottub hottub; public HottubOffCommand(Hottub hottub){ this.hottub=hottub; } public void execute(){ hottub.off(); } public void undo(){ hottub.on(); } } class LightOffCommand implements Command{ Light light; public LightOffCommand(Light light){ this.light=light; } public void execute(){ light.off(); } public void undo(){ light.on(); } } class CeilingFanOnCommand implements Command{ CeilingFan ceilingFan; public CeilingFanOnCommand(CeilingFan ceilingFan){ this.ceilingFan=ceilingFan; } public void execute(){ ceilingFan.on(); } public void undo(){ ceilingFan.off(); } } class CeilingFanHighCommand implements Command{ CeilingFan ceilingFan; int prevSpeed; public CeilingFanHighCommand(CeilingFan ceilingFan){ this.ceilingFan=ceilingFan; } public void execute(){ prevSpeed=ceilingFan.getSpeed(); ceilingFan.high(); } public void undo(){ if(prevSpeed==CeilingFan.HIGH){ ceilingFan.high(); } if(prevSpeed==CeilingFan.MEDIUM){ ceilingFan.medium(); } if(prevSpeed==CeilingFan.LOW){ ceilingFan.low(); } if(prevSpeed==CeilingFan.OFF){ ceilingFan.off(); } } } class CeilingFanMediumCommand implements Command{ CeilingFan ceilingFan; int prevSpeed; public CeilingFanMediumCommand(CeilingFan ceilingFan){ this.ceilingFan=ceilingFan; } public void execute(){ prevSpeed=ceilingFan.getSpeed(); ceilingFan.medium(); } public void undo(){ if(prevSpeed==CeilingFan.HIGH){ ceilingFan.high(); } if(prevSpeed==CeilingFan.MEDIUM){ ceilingFan.medium(); } if(prevSpeed==CeilingFan.LOW){ ceilingFan.low(); } if(prevSpeed==CeilingFan.OFF){ ceilingFan.off(); } } } class CeilingFanLowCommand implements Command{ CeilingFan ceilingFan; int prevSpeed; public CeilingFanLowCommand(CeilingFan ceilingFan){ this.ceilingFan=ceilingFan; } public void execute(){ prevSpeed=ceilingFan.getSpeed(); ceilingFan.low(); } public void undo(){ if(prevSpeed==CeilingFan.HIGH){ ceilingFan.high(); } if(prevSpeed==CeilingFan.MEDIUM){ ceilingFan.medium(); } if(prevSpeed==CeilingFan.LOW){ ceilingFan.low(); } if(prevSpeed==CeilingFan.OFF){ ceilingFan.off(); } } } class CeilingFanOffCommand implements Command{ CeilingFan ceilingFan; int prevSpeed; public CeilingFanOffCommand(CeilingFan ceilingFan){ this.ceilingFan=ceilingFan; } public void execute(){ prevSpeed=ceilingFan.getSpeed(); ceilingFan.off(); } public void undo(){ if(prevSpeed==CeilingFan.HIGH){ ceilingFan.high(); } if(prevSpeed==CeilingFan.MEDIUM){ ceilingFan.medium(); } if(prevSpeed==CeilingFan.LOW){ ceilingFan.low(); } if(prevSpeed==CeilingFan.OFF){ ceilingFan.off(); } } } class GarageDoorUpCommand implements Command{ GarageDoor garageDoor; public GarageDoorUpCommand(GarageDoor garageDoor){ this.garageDoor=garageDoor; } public void execute(){ garageDoor.up(); } public void undo(){ garageDoor.down(); } } class GarageDoorDownCommand implements Command{ GarageDoor garageDoor; public GarageDoorDownCommand(GarageDoor garageDoor){ this.garageDoor=garageDoor; } public void execute(){ garageDoor.down(); } public void undo(){ garageDoor.up(); } } class StereOnCommand implements Command{ Stereo stereo; public StereOnCommand(Stereo stereo){ this.stereo=stereo; } public void execute(){ stereo.on(); } public void undo(){ stereo.off(); } } class StereOnWithCDCommand implements Command{ Stereo stereo; public StereOnWithCDCommand(Stereo stereo){ this.stereo=stereo; } public void execute(){ stereo.on(); stereo.setCD(); stereo.setVolume(11); } public void undo(){ stereo.off(); } } class StereOffCommand implements Command{ Stereo stereo; public StereOffCommand(Stereo stereo){ this.stereo=stereo; } public void execute(){ stereo.off(); } public void undo(){ stereo.on(); stereo.setCD(); stereo.setVolume(11); } } class GarageDoorOpenCommand implements Command{ GarageDoor garageDoor; public GarageDoorOpenCommand(GarageDoor garageDoor){ this.garageDoor=garageDoor; } public void execute(){ this.garageDoor.lightOn(); } public void undo(){ garageDoor.lightOff(); } } class NoCommand implements Command{ public void execute(){} public void undo(){} } class SimpleRemoteControl{ Command slot; public SimpleRemoteControl(){} public void setCommand(Command command){ slot=command; } public void bottomWasPressed(){ slot.execute(); } } class RemoteControl{ Command[] onCommands; Command[] offCommands; 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; } } public void setCommand(int slot,Command onCommand,Command offCommand){ onCommands[slot]=onCommand; offCommands[slot]=offCommand; } public void onButtonWasPushed(int slot){ onCommands[slot].execute(); } public void offButtonWasPushed(int slot){ offCommands[slot].execute(); } public String toString(){ StringBuffer stringBuff=new StringBuffer(); stringBuff.append("\n------ Remote Control ------\n"); for(int i=0; i