宏命令:设计模式 - 命令模式(command pattern) 宏命令(macro command) 详解

命令模式(command pattern) 宏命令(macro command) 详解

 

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

 

参考:命名模式(撤销): http://blog.csdn.net/caroline_wendy/article/details/31419101

 

命令模式可以执行宏命令(macro command), 即多个命令的组合操作.

 

具体方法: 

1. 其余代码与命令(撤销)一致

2. 添加宏命令(macro command), 继承命令接口, 包含命令数组(Command[]), 依次执行(execute)或撤销(undo)命令.

 

/** * @time 2014年6月16日 */package command;/** * @author C.L.Wang * */public class MecroCommand implements Command {Command[] commands;public MecroCommand (Command[] commands) {this.commands = commands;}/* (non-Javadoc) * @see command.Command#execute() */@Overridepublic void execute() {// TODO Auto-generated method stubfor (int i=0; i<commands.length; ++i) {commands[i].execute();}}/* (non-Javadoc) * @see command.Command#undo() */@Overridepublic void undo() {// TODO Auto-generated method stubfor (int i = commands.length-1; i >= 0; i--) {commands[i].undo();}}}


3.测试, 把多个命令, 放入命令数组, 初始化宏类.

 

 

/** * @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");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);CeilingFanHighCommand ceilingFanHigh = new CeilingFanHighCommand(ceilingFan);CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan);StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo);StereoOffCommand stereoOffCommand = new StereoOffCommand(stereo);Command[] partyOn = { livingRoomLightOn, kitchenLightOn, ceilingFanHigh, stereoOnWithCD };Command[] partyOff = { livingRoomLightOff, kitchenLightOff, ceilingFanOff, stereoOffCommand };MecroCommand partyOnMecro = new MecroCommand(partyOn);MecroCommand partyOffMecro = new MecroCommand(partyOff);remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff); //设这遥控器remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);remoteControl.setCommand(2, ceilingFanHigh, ceilingFanOff);remoteControl.setCommand(3, stereoOnWithCD, stereoOffCommand);remoteControl.setCommand(4, partyOnMecro, partyOffMecro);System.out.println(remoteControl);System.out.println("--- Pushing Macro On ---");remoteControl.onButtonWasPushed(4); //关闭高速System.out.println("--- Pushing Macro Off ---");remoteControl.offButtonWasPushed(4); //关闭高速System.out.println("--- Pushing Macro Undo ---");remoteControl.undoButtonWasPushed(); //退回高速}}


4. 输出:

 

 

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

 

 

 

 

 

 

 

 

相关推荐

相关文章