第一个命令对象
-- 实现命令接口
public interface Command ... {
public void execute();
}
-- 实现一个打开电灯的命令
public class LightOnCommand implemetns Command ... {
Light light;
public LightOnCommand(Light light) ...{
this.light = light;
}
public void execute() ...{
light.on();
}
}
使用命令对象
public class SimpleRemoteControl ... {
Command slot;
public SimpleRemoteContronl() ...{}
public void setCommand(Command command) ...{
slot = command;
}
public void buttonWasPressed() ...{
slot.execute();
}
}
遥控器使用的简单测试
public class RemoteControlTest ... {
public static void main(String[] args) ...{
SimpleRemoteContron remote = new SimpleRemoteControl();
Light light = new Light();
LightOnCommand lightOn = new LightOnCommand(light);
remote.setComand(lightOn);
remote.buttonWasPressed();
}
}
Sharpen your pencil
public class GarageDoorOpenComand implements Command ... {
GarageDoor = garageDoor;
public GarageDoorOpenCommand(GarageDoor garageDoor) ...{
this.garageDoor = garageDoor;
}
public void execute() ...{
garageDoor.up();
}
}
public class RemoteControlTest ... {
public static void main(String[] args) ...{
SimpleRemoteContronl remote = new SimpleRemoteControl();
Light light = new Light();
GarageDoor garageDoor = new GarageDoor();
LightOnCommand lightOn = new LightOnCommand(light);
GarageDoorOpenCommand garageOpen = new GarageDoorOpenCommand(garageDoor);
remote.setCommand(lightOn);
remote.buttonWasPressed();
remote.setCommand(garageOpen);
remote.buttonWasPressed();
}
}
定义命令模式
命令模式:将“请求”封装成对象,以便使用不同的请求,队列或者日志来参数化
其他对象。命令模式也支持可撤销的操作。
命令模式的设计如何支持请求调用者和请求接收者之间的解耦?
实现遥控器
public 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 stringBuffer = new StringBuffer();
stringBuffer.append(" ----------Remote Control---------");
for (int i=0; i<onCommands.length; i++) ...{
stringBuffer.append("[slot" + i + "]" + onCommands[i].getClass().getName()
+ " " + offCommands[i].getClass().getName() + " ");
}
return stringBuffer.toString();
}
}
实现命令
public class LightOffCommand implements Command ... {
Light light;
public LightOffCommand(Light light) ...{
this.light = light;
}
public void execute() ...{
light.off();
}
}
public class StereoOnWithCDCommand implements Command ... {
Stereo stereo;
public StereoOnWithCDCommand(Stereo stereo) ...{
this.stereo = stereo;
}
public void execute() ...{
stereo.on();
stereo.setCD();
stereo.setVolume(11);
}
}
逐步测试遥控器
public class RemoteLoader ... {
public static void main(String[] args) ...{
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);
……
}
}
Undo的2种;Party模式“宏”;队列请求;日志请求……
-- 实现命令接口
public interface Command ... {
public void execute();
}
-- 实现一个打开电灯的命令
public class LightOnCommand implemetns Command ... {
Light light;
public LightOnCommand(Light light) ...{
this.light = light;
}
public void execute() ...{
light.on();
}
}
使用命令对象
public class SimpleRemoteControl ... {
Command slot;
public SimpleRemoteContronl() ...{}
public void setCommand(Command command) ...{
slot = command;
}
public void buttonWasPressed() ...{
slot.execute();
}
}
遥控器使用的简单测试
public class RemoteControlTest ... {
public static void main(String[] args) ...{
SimpleRemoteContron remote = new SimpleRemoteControl();
Light light = new Light();
LightOnCommand lightOn = new LightOnCommand(light);
remote.setComand(lightOn);
remote.buttonWasPressed();
}
}
Sharpen your pencil
public class GarageDoorOpenComand implements Command ... {
GarageDoor = garageDoor;
public GarageDoorOpenCommand(GarageDoor garageDoor) ...{
this.garageDoor = garageDoor;
}
public void execute() ...{
garageDoor.up();
}
}
public class RemoteControlTest ... {
public static void main(String[] args) ...{
SimpleRemoteContronl remote = new SimpleRemoteControl();
Light light = new Light();
GarageDoor garageDoor = new GarageDoor();
LightOnCommand lightOn = new LightOnCommand(light);
GarageDoorOpenCommand garageOpen = new GarageDoorOpenCommand(garageDoor);
remote.setCommand(lightOn);
remote.buttonWasPressed();
remote.setCommand(garageOpen);
remote.buttonWasPressed();
}
}
定义命令模式
命令模式:将“请求”封装成对象,以便使用不同的请求,队列或者日志来参数化
其他对象。命令模式也支持可撤销的操作。
命令模式的设计如何支持请求调用者和请求接收者之间的解耦?
实现遥控器
public 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 stringBuffer = new StringBuffer();
stringBuffer.append(" ----------Remote Control---------");
for (int i=0; i<onCommands.length; i++) ...{
stringBuffer.append("[slot" + i + "]" + onCommands[i].getClass().getName()
+ " " + offCommands[i].getClass().getName() + " ");
}
return stringBuffer.toString();
}
}
实现命令
public class LightOffCommand implements Command ... {
Light light;
public LightOffCommand(Light light) ...{
this.light = light;
}
public void execute() ...{
light.off();
}
}
public class StereoOnWithCDCommand implements Command ... {
Stereo stereo;
public StereoOnWithCDCommand(Stereo stereo) ...{
this.stereo = stereo;
}
public void execute() ...{
stereo.on();
stereo.setCD();
stereo.setVolume(11);
}
}
逐步测试遥控器
public class RemoteLoader ... {
public static void main(String[] args) ...{
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);
……
}
}
Undo的2种;Party模式“宏”;队列请求;日志请求……