设计模式

命令模式(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()
	 */
	@Override
	public void execute() {
		// TODO Auto-generated method stub
		for (int i=0; i<commands.length; ++i) {
			commands[i].execute();
		}
	}

	/* (non-Javadoc)
	 * @see command.Command#undo()
	 */
	@Override
	public void undo() {
		// TODO Auto-generated method stub
		for (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 stub

		RemoteControl 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 on
Kitchen Light is on
Living Room ceiling fan is on high
Living Room stereo is on
Living Room stereo is set for CD input
Living Room Stereo volume set to 11
--- Pushing Macro Off ---
Living Room Light is off
Kitchen Light is off
Living Room ceiling fan is off
Living Room stereo is off
--- Pushing Macro Undo ---
Living Room stereo is on
Living Room stereo is set for CD input
Living Room Stereo volume set to 11
Living Room ceiling fan is on high
Kitchen Light is on
Living Room Light is on


设计模式