设计模式 - 命令模式(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()
	 */
	@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

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ElminsterAumar

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值