设计模式:命令
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); } }
这种命令模式把所有命令进行抽象,然后用不同具体指令实现。可方便灵活更换指令。
相关文章推荐
- 闲聊设计模式和类
- 好书整理系列之-设计模式:可复用面向对象软件的基础 5
- 好书整理系列之-设计模式:可复用面向对象软件的基础 4
- 好书整理系列之-设计模式:可复用面向对象软件的基础 3
- 好书整理系列之-设计模式:可复用面向对象软件的基础 2
- 好书系列之-设计模式:可复用面向对象软件的基础 1
- 好书整理系列之-设计模式:可复用面向对象软件的基础 5.5
- 设计模式、用Delphi描述-->Abstract Factory模式
- 设计模式、用Delphi描述-->Factory Method模式
- 设计模式、用Delphi实现---->Strategy模式
- 与大虾对话: 领悟设计模式
- 好书整理系列之-设计模式:可复用面向对象软件的基础 6
- 设计模式入门(1)------单件(Singleton)
- 设计模式、用Delphi描述-->Reference Count pattern
- Engine-Collection-Class,一种用来建立可重用企业组件的设计模式
- 好书整理系列之-设计模式:可复用面向对象软件的基础 7
- 好书整理系列之-设计模式:可复用面向对象软件的基础 8
- Jive 中的设计模式
- 设计模式、用Delphi描述-->Visitor模式
- 设计模式、用Delphi实现---->Template Method模式