设计模式: 自己手动写一个命令模式

来源:互联网 发布:javascript map reduce 编辑:程序博客网 时间:2024/05/16 14:40

.命令模式: 将“请求”封装成对象,以便使用不同的请求、队列、日志来参数化其他对象。命令模式也支持可撤销的操作。

  命令对象将动作和接收者(当作的执行者)包进对象中。这个对象之暴露一个execute()方法. 命令也可以用来实现日志和事务系统。宏命令是命令的一种简单延伸,允许调用多个命令。


下面是命令模式的一个类图:



一个案例的源代码如下:

package command;/** * 命令对象接口 * @author Arvon * */public interface Command {public void execute();public void undo();}

package command;public class LightOnCommand implements Command {private Light light;public LightOnCommand(Light light) {super();this.light = light;}@Overridepublic void execute() {// TODO Auto-generated method stublight.on();}@Overridepublic void undo() {// TODO Auto-generated method stublight.off();}}

package command;public class LightOffCommand implements Command {private Light light;public LightOffCommand(Light light) {super();this.light = light;}@Overridepublic void execute() {// TODO Auto-generated method stublight.off();}@Overridepublic void undo() {// TODO Auto-generated method stublight.on();}}


package command;public class CeillingFanHighCommand implements Command {private CeilingFan fan;private int prevSpeed;@Overridepublic void execute() {prevSpeed = fan.getSpeed();fan.high();}public CeillingFanHighCommand(CeilingFan fan) {super();this.fan = fan;}@Overridepublic void undo() {if (prevSpeed == CeilingFan.HIGH) {fan.high();} else if (prevSpeed == CeilingFan.MEDIUM) {fan.medium();} else if (prevSpeed == CeilingFan.LOW) {fan.low();} else if (prevSpeed == CeilingFan.OFF) {fan.off();}}}

package command;public class CeillingFanOffCommand implements Command {CeilingFan ceilingFan;int prevSpeed;  public CeillingFanOffCommand(CeilingFan ceilingFan) {this.ceilingFan = ceilingFan;}@Overridepublic void execute() {// TODO Auto-generated method stubprevSpeed = ceilingFan.getSpeed();ceilingFan.off();}@Overridepublic void undo() {// TODO Auto-generated method stubif (prevSpeed == CeilingFan.HIGH) {ceilingFan.high();} else if (prevSpeed == CeilingFan.MEDIUM) {ceilingFan.medium();} else if (prevSpeed == CeilingFan.LOW) {ceilingFan.low();} else if (prevSpeed == CeilingFan.OFF) {ceilingFan.off();}}}


package command;/** * 什么都不做 空对象 * @author Administrator * */public class NoCommand implements Command {@Overridepublic void execute() {// TODO Auto-generated method stub}@Overridepublic void undo() {// TODO Auto-generated method stub}}



package command;/** * 遥控器 Invoker 管理许多command对象 * @author Arvon * */public class RemoteControl {Command[] onCommands;Command[] offCommands;/** * 记录前一个命令 */Command undoCommand;/** * 初始化所有命令 */public RemoteControl() {super();onCommands = new Command[5];offCommands = new Command[5];for(int i=0;i<5;i++){Command noCommand = new NoCommand();onCommands[i] = noCommand ;offCommands[i] = noCommand ;}}/** * 设置命令 * @param slot 第几个插槽 * @param onCommand * @param offCommand */public void setCommand(int slot,Command onCommand, Command offCommand){onCommands[slot] = onCommand;offCommands[slot] = offCommand;}public void onButtonWasPush(int slot){onCommands[slot].execute();undoCommand = onCommands[slot];}public void offButtonWasPush(int slot){offCommands[slot].execute();undoCommand = offCommands[slot];}public void undoButtonWasPush(){if(undoCommand!=null)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().getSimpleName()+ "    " + offCommands[i].getClass().getSimpleName() + "\n");}stringBuff.append("[undo] " + undoCommand.getClass().getSimpleName() + "\n");return stringBuff.toString();}}


package command;/** * 灯 命令的执行者 * @author Administrator * */public class Light {public void on() {// TODO Auto-generated method stubSystem.out.println("light is on...");}public void off() {// TODO Auto-generated method stubSystem.out.println("light is off...");}}


package command;/** * 电风扇 命令的执行者 * @author Administrator * */public 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 CeilingFan(String location) {this.location = location;speed = OFF;}  public void high() {speed = HIGH;System.out.println(location + " ceiling fan is on high");}  public void medium() {speed = MEDIUM;System.out.println(location + " ceiling fan is on medium");} public void low() {speed = LOW;System.out.println(location + " ceiling fan is on low");}  public void off() {speed = OFF;System.out.println(location + " ceiling fan is off");}  public int getSpeed() {return speed;}}


package command;/** * 测试类 * @author Administrator * */public class CommandTest {public static void main(String[] args) {// TODO Auto-generated method stubRemoteControl control = new RemoteControl(); Light light = new Light(); Command lightOn = new LightOnCommand(light); Command lightOff = new LightOffCommand(light); control.setCommand(0, lightOn, lightOff ); control.onButtonWasPush(0); control.offButtonWasPush(0); control.undoButtonWasPush(); CeilingFan fan = new CeilingFan("living room"); Command mCeillingFanHighCommand = new CeillingFanHighCommand(fan); Command CeillingFanOffCommand = new CeillingFanOffCommand(fan); control.setCommand(1, mCeillingFanHighCommand, CeillingFanOffCommand); control.onButtonWasPush(1); control.offButtonWasPush(1); control.onButtonWasPush(1); control.undoButtonWasPush(); }}


程序的输出:

light is on...
light is off...
light is on...
living room ceiling fan is on high
living room ceiling fan is off
living room ceiling fan is on high
living room ceiling fan is off



0 0