设计模式 - 命令模式(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) : 将请求封装成对 ...
随机推荐
- CSS小技巧-图片自动缩放
css的一个重要属性:max-width min-width 示例: <div width="500" height="259"><p> ...
- vagrant 使用方法
0.介绍 Vagrant 是一款用来构建虚拟开发环境的工具,非常适合 php/python/ruby/java 这类语言开发 web 应用,"代码在我机子上运行没有问题"这种说辞将 ...
- 关于Mysql索引的笔记
MySQL索引原理 索引目的 索引的目的在于提高查询效率,可以类比字典,如果要查“mysql”这个单词,我们肯定需要定位到m字母,然后从下往下找到y字母,再找到剩下的sql.如果没有索引,那么你可能需 ...
- this class is not key value coding-compliant for the key detailItem
我出这个错误是因为,自己的一个视图没有给指定想用的viewController文件..及一个classController控制一个xib文件,忘给该视图指定控制器了.
- float与position
使用float会使块级元素的宽高表现为包裹内容(在不设定宽高的情况下) 这是当然的 我们使用float就是使几个div排在一行 当然不可能在宽度上撑满父元素啦 至于高度 不论有没有float 高 ...
- 蝕刻技術(Etching Technology)
1. 前言 蚀刻是将材料使用化学反应或物理撞击作用而移除的技术. 蚀刻技术可以分为『湿蚀刻』(wet etching)及『干蚀刻』(dry etching)两类.在湿蚀刻中是使用化学溶液,经由化学反应 ...
- linux基础随笔
磁盘管理 sda s:磁盘接口的类型(sata scsci sas) d:驱动器(drive) a:(第一块磁盘,同理b第二块磁盘)hda h:ide接口 第一块磁盘的第一个分区:sda1 mount ...
- Maven 版 JPA 最佳实践
项目结构图 数据库环境 数据库:MySQL 版本:5.x 数据库名:jpa-demo 用户名密码:root/1234 代码清单 1:数据库脚本: /* Navicat MySQL Data Trans ...
- use utf8
[root@wx03 0724]# cat a2.pl use Encode; my $a=<STDIN>; my $b=encode_utf8('微信'); print "\$ ...
- Deep Learning论文笔记之(六)Multi-Stage多级架构分析
Deep Learning论文笔记之(六)Multi-Stage多级架构分析 zouxy09@qq.com http://blog.csdn.net/zouxy09 自己平时看了一些 ...