1
。命令模式将发出请求的对象和执行请求的对象解耦。
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<onCommands.length;i++){
stringBuff.append("[slot "+i+"]"+onCommands[i].getClass().getName()+" "+offCommands[i].getClass().getName() +"\n");
}
return stringBuff.toString();
}
}
class RemoteControlWithUndo{
Command[] onCommands;
Command[] offCommands;
Command undoCommand;
public RemoteControlWithUndo(){
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");
}
return stringBuff.toString();
}
}
class MacroCommand implements Command{
Command[] commands;
public MacroCommand(Command[] commands){
this.commands=commands;
}
public void execute(){
for (int i=0;i<commands.length;i++){
commands[i].execute();
}
}
public void undo(){
for (int i=0;i<commands.length;i++){
commands[i].undo();
}
}
}
package pattern;
public class RemoteLoader {
/**
* @param args
*/
public static void main(String[] args) {
RemoteControlWithUndo remoteControlundo=new RemoteControlWithUndo();
RemoteControl remoteControl=new RemoteControl();
Light livingRoomLight=new Light("Living Room");
Light kitchenLight=new Light("Kitchen");
CeilingFan ceilingFan=new CeilingFan("Living Room");
GarageDoor garageDoor=new GarageDoor("");
Stereo stereo=new Stereo("Living Room");
LightOnCommand livingRoomLightOn=new LightOnCommand(livingRoomLight);
LightOffCommand livingRoomLightOff=new LightOffCommand(livingRoomLight);
LightOnCommand kitchenLightOn=new LightOnCommand(kitchenLight);
LightOffCommand kitchenLightOff=new LightOffCommand(kitchenLight);
CeilingFanOnCommand ceilingFanOn=new CeilingFanOnCommand(ceilingFan);
CeilingFanOffCommand ceilingFanOff=new CeilingFanOffCommand(ceilingFan);
GarageDoorUpCommand garageDoorUp=new GarageDoorUpCommand(garageDoor);
GarageDoorDownCommand garageDoorDown=new GarageDoorDownCommand(garageDoor);
StereOnWithCDCommand stereoOnWithCD= new StereOnWithCDCommand(stereo);
StereOffCommand stereoOff=new StereOffCommand(stereo);
remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);
remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff);
remoteControl.setCommand(3, stereoOnWithCD, stereoOff);
System.out.println(remoteControl);
remoteControl.onButtonWasPushed(0);
remoteControl.offButtonWasPushed(0);
remoteControl.onButtonWasPushed(1);
remoteControl.offButtonWasPushed(1);
remoteControl.onButtonWasPushed(2);
remoteControl.offButtonWasPushed(2);
remoteControl.onButtonWasPushed(3);
remoteControl.offButtonWasPushed(3);
remoteControlundo.setCommand(0, livingRoomLightOn, livingRoomLightOff);
remoteControlundo.onButtonWasPushed(0);
remoteControlundo.offButtonWasPushed(0);
System.out.println(remoteControlundo);
remoteControlundo.undoButtonWasPushed();
remoteControlundo.offButtonWasPushed(0);
remoteControlundo.onButtonWasPushed(0);
System.out.println(remoteControlundo);
remoteControlundo.undoButtonWasPushed();
System.out.println("ceilingFan high beging ......");
CeilingFanMediumCommand ceilingFanMedium=new CeilingFanMediumCommand(ceilingFan);
CeilingFanHighCommand ceilingFanHigh=new CeilingFanHighCommand(ceilingFan);
remoteControlundo.setCommand(0, ceilingFanMedium, ceilingFanOff);
remoteControlundo.setCommand(1, ceilingFanHigh, ceilingFanOff);
remoteControlundo.onButtonWasPushed(0);
remoteControlundo.offButtonWasPushed(0);
System.out.println(remoteControlundo);
remoteControlundo.undoButtonWasPushed();
remoteControlundo.onButtonWasPushed(1);
System.out.println(remoteControlundo);
remoteControlundo.undoButtonWasPushed();
System.out.println("hong command begin ......");
Light light =new Light("Living Room");
TV tv=new TV("Living Room");
Hottub hottub=new Hottub();
LightOnCommand lightOn=new LightOnCommand(light);
StereOnCommand stereoOn=new StereOnCommand(stereo);
TVOnCommand tvOn=new TVOnCommand(tv);
HottubOnCommand hottubOn=new HottubOnCommand(hottub);
LightOffCommand lightOff=new LightOffCommand(light);
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);
remoteControlundo.setCommand(0, partyOnMacro, partyOffMacro);
System.out.println(remoteControlundo);
System.out.println("---- Pushing Macro On---");
remoteControlundo.onButtonWasPushed(0);
System.out.println("---- Pushing Macro Off---");
remoteControlundo.offButtonWasPushed(0);
}
}