设计模式 - 命令模式(command pattern) 多命令 详解

来源:互联网 发布:明保理和暗保理 知乎 编辑:程序博客网 时间:2024/06/06 09:56

命令模式(command pattern) 多命令 详解


本文地址: http://blog.csdn.net/caroline_wendy


参考命令模式: http://blog.csdn.net/caroline_wendy/article/details/31379977


具体步骤:

1. 多命令, 把未使用的命令, 初始化为空对象(NoCommand), 根据参数(slot), 选择输出命令.

/** * @time 2014年6月16日 */package command;/** * @author C.L.Wang * */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) {this.onCommands[slot] = onCommand;this.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("\n------ Remote Control ------\n");for (int i=0; i<onCommands.length; ++i) {stringBuffer.append("[slot " + i + "] " + onCommands[i].getClass().getName()+ "    " + offCommands[i].getClass().getName() + "\n");}return stringBuffer.toString();}}

2. 空命令(NoCommand)对象, 继承命令接口.

package command;public class NoCommand implements Command {public void execute() { }}

3. 测试对象, 分别给多命令赋值(setCommand), 通过参数(slot)调用.

/** * @time 2014年6月16日 */package command;import javax.crypto.spec.IvParameterSpec;/** * @author C.L.Wang * */public class RemoteLoader {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubRemoteControl 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);GarageDoorOnCommand garageDoorOn = new GarageDoorOnCommand(garageDoor);GarageDoorOffCommand garageDoorOff = new GarageDoorOffCommand(garageDoor);StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo);StereoOffCommand stereoOffCommand = new StereoOffCommand(stereo);remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff);remoteControl.setCommand(3, stereoOnWithCD, stereoOffCommand);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);}}


4. 输出:

------ Remote Control ------[slot 0] command.LightOnCommand    command.LightOffCommand[slot 1] command.LightOnCommand    command.LightOffCommand[slot 2] command.CeilingFanOnCommand    command.CeilingFanOffCommand[slot 3] command.StereoOnWithCDCommand    command.StereoOffCommand[slot 4] command.NoCommand    command.NoCommand[slot 5] command.NoCommand    command.NoCommand[slot 6] command.NoCommand    command.NoCommandLiving Room Light is onLiving Room Light is offKitchen Light is onKitchen Light is offLiving Room ceiling fan is on highLiving Room ceiling fan is offLiving Room stereo is onLiving Room stereo is set for CD inputLiving Room Stereo volume set to 11Living Room stereo is off



其余代码下载: http://download.csdn.net/detail/u012515223/7506695






7 0
原创粉丝点击