您的位置:首页 > 其它

设计模式:命令

2016-12-15 00:00 155 查看
下面模拟一个场景,一个遥控器空置诸如卧室灯、客厅灯、电视等等设备的开关。

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);
//cd播放器开指令
StereoOnWithCDCommand stereoOnWithCD =
new StereoOnWithCDCommand(stereo);
//播放器关指令。
StereoOffCommand  stereoOff =
new StereoOffCommand(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);
}
}


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 voi
7fe0
d 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();
}
}

public class NoCommand implements Command {
public void execute() { }
}

public class Light {
String location = "";

public Light(String location) {
this.location = location;
}

public void on() {
System.out.println(location + " light is on");
}

public void off() {
System.out.println(location + " light is off");
}
}

public class LightOffCommand implements Command {
Light light;

public LightOffCommand(Light light) {
this.light = light;
}

public void execute() {
light.off();
}
}

public class LightOnCommand implements Command {
Light light;

public LightOnCommand(Light light) {
this.light = light;
}

public void execute() {
light.on();
}
}

public class LivingroomLightOffCommand implements Command {
Light light;

public LivingroomLightOffCommand(Light light) {
this.light = light;
}

public void execute() {
light.off();
}
}

public class LivingroomLightOnCommand implements Command {
Light light;

public LivingroomLightOnCommand(Light light) {
this.light = light;
}

public void execute() {
light.on();
}
}

public class CeilingFan {
String location = "";
int level;
public static final int HIGH = 2;
public static final int MEDIUM = 1;
public static final int LOW = 0;

public CeilingFan(String location) {
this.location = location;
}

public void high() {
// turns the ceiling fan on to high
level = HIGH;
System.out.println(location + " ceiling fan is on high");

}

public void medium() {
// turns the ceiling fan on to medium
level = MEDIUM;
System.out.println(location + " ceiling fan is on medium");
}

public void low() {
// turns the ceiling fan on to low
level = LOW;
System.out.println(location + " ceiling fan is on low");
}

public void off() {
// turns the ceiling fan off
level = 0;
System.out.println(location + " ceiling fan is off");
}

public int getSpeed() {
return level;
}
}

public class Stereo {
String location;

public Stereo(String location) {
this.location = location;
}

public void on() {
System.out.println(location + " stereo is on");
}

public void off() {
System.out.println(location + " stereo is off");
}

public void setCD() {
System.out.println(location + " stereo is set for CD input");
}

public void setDVD() {
System.out.println(location + " stereo is set for DVD input");
}

public void setRadio() {
System.out.println(location + " stereo is set for Radio");
}

public void setVolume(int volume) {
// code to set the volume
// valid range: 1-11 (after all 11 is better than 10, right?)
System.out.println(location + " Stereo volume set to " + volume);
}
}

public class StereoOffCommand implements Command {
Stereo stereo;

public StereoOffCommand(Stereo stereo) {
this.stereo = stereo;
}

public void execute() {
stereo.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);
}
}

这种命令模式把所有命令进行抽象,然后用不同具体指令实现。可方便灵活更换指令。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: