码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-06-17 23:14:44      阅读:324      评论:0      收藏:0      [点我收藏+]

标签:mystra   设计模式   命令模式   宏命令   java   

命令模式(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


bubuko.com,布布扣



设计模式 - 命令模式(command pattern) 宏命令(macro command) 详解,布布扣,bubuko.com

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

标签:mystra   设计模式   命令模式   宏命令   java   

原文地址:http://blog.csdn.net/caroline_wendy/article/details/31446423

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!