设计模式 - 命令模式(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
设计模式 - 命令模式(command pattern) 宏命令(macro command) 具体解释的更多相关文章
- 设计模式 - 迭代模式(iterator pattern) Java 迭代器(Iterator) 详细解释
迭代模式(iterator pattern) Java 迭代器(Iterator) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 參考迭代器模式(ite ...
- 深入浅出设计模式——命令模式(Command Pattern)
模式动机 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需在程序运行时指定具体的请求接收者即可,此时,可以使用命令模式来进行设计,使得请 ...
- javascript设计模式——命令模式
前面的话 假设有一个快餐店,而我是该餐厅的点餐服务员,那么我一天的工作应该是这样的:当某位客人点餐或者打来订餐电话后,我会把他的需求都写在清单上,然后交给厨房,客人不用关心是哪些厨师帮他炒菜.餐厅还可 ...
- 【设计模式】Java设计模式 - 命令模式
Java设计模式 - 命令模式 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 目录 Ja ...
- c#设计模式-命令模式
一. 命令(Command)模式 命令(Command)模式属于对象的行为模式[GOF95].命令模式又称为行动(Action)模式或交易(Transaction)模式.命令模式把一个请求或者操作封装 ...
- linkin大话设计模式--命令模式
linkin大话设计模式--命令模式 首先考虑一种应用情况,某个方法需要完成某一个功能,这个功能的大部分功能已经确定了,但是有可能少量的步骤没法确定,必须等到执行这个方法才可以确定. 也就是说,我们写 ...
- 设计模式 - 命令模式(command pattern) 多命令 具体解释
命令模式(command pattern) 多命令 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.csdn.ne ...
- 24种设计模式--命令模式【Command Pattern】
今天讲命令模式,这个模式从名字上看就很简单,命令嘛,老大发命令,小兵执行就是了,确实是这个意思,但是更深化了,用模式来描述真实世界的命令情况.正在看这本书的你,我猜测分为两类:已经工作的和没有工作的, ...
- 设计模式 - 命令模式(command pattern) 具体解释
命令模式(command pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 命令模式(command pattern) : 将请求封装成对 ...
随机推荐
- apache下的IfModule里设置含义
<IfModule mod_deflate.c> SetOutputFilter DEFLATE #必须的,就像一个开关一样,告诉apache对传输到浏览器的内容进行压缩 SetEnvIf ...
- C语言之猜数字游戏
猜数字游戏 猜数字游戏是以前功能机上的一款益智游戏,计算机会根据输入的位数随机分配一个符合要求的数据,计算机输出guess后便可以输入数字,注意数字间需要用空格或回车符加以区分,计算机会根据输入信息给 ...
- oracle 10g函数大全--分析函数
oracle分析函数--SQL*PLUS环境 一.总体介绍 12.1 分析函数如何工作 语法 FUNCTION_NAME(<参数>,…) OVER (<PARTITION BY 表达 ...
- QT基本数据类型(以前没见过qintptr和qlonglong)
QT的基本数据类型 qint8:signed char 有符号8比特数据 qint16:signed short 16位数据类型 qint32:signed int. 32位有符号数据类型 qint6 ...
- VJGUI消息设计-兼谈MFC、QT和信号/槽机制
星期六下午4点,还在公司加班.终于写完了下周要交工的一个程序. 郁闷,今天这几个小时写了有上千行代码吧?虽然大部分都是Ctrl-C+Ctrl-V,但还是郁闷. 作为一个有10年经验的MFC程序员,郁闷 ...
- Windows failed to start.界面下修复win8引导
首先要保证 系统本身是没有问题的 不是在装机的时候出现这种情况 那么可以按照以下方法来进行 首先要在另外一台电脑上将win8刻进u盘 启动时以u盘为第一启动项启动 进入win8装机界面 点击左下角的修 ...
- <jsp:include page="">和<%@include page=""%> 标签学习
<jsp:include page=""><jsp:param value=""name=""/><DEL&g ...
- js php xmlrequest 上传图片
本来想用插件上传图片的,后来自己写了一个简单的js实现异步的图片上传,不多说上代码很easy upload.php <?php if(isset($_FILES["myfile&quo ...
- LINQ简单案例
1.在visual studio 创建一个解决方案,新建一个控制台程序Kong 2.新建两个类,分别为Master 类和Kongfu类 Master类中包含成员如下,并重写ToString方法 na ...
- java 如何自定义异常 用代码展示 真心靠谱
先建两个自定义的异常类 ChushufuException类 class ChushufuException extends Exception { public ChushufuException( ...