设计模式 - 命令模式(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) : 将请求封装成对 ...
随机推荐
- 51cto运维培训课程
线路图: Linux运维架构师 基础提高篇(120课时) 基础提高篇(240课时) Linux系统基础及系统管理 Shell编程入门及进阶 linux安全管理和企业级安全防范策略 企业级集群/存储专题 ...
- Windows XP硬盘安装Ubuntu 12.04双系统图文详解
Windows XP硬盘安装Ubuntu 12.04双系统图文详解 Ubuntu 12.04 LTS版本于2012年4月26日发布,趁着五一放假,赶紧在自己的Windows XP的电脑上安装下Ubun ...
- Drainage Ditches(最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64044 Accepted: 2471 ...
- CentOS6.4关闭触控板
1. 检查是否安装xorg-x11-app; rpm -qa xorg-x11-apps 如果没有安装使用下面命令安装xorg-x11-app yum install xorg-x11-apps 2. ...
- Android 开发之网易云音乐(或QQ音乐)的播放界面转盘和自定义SeekBar的实现
这个东西我在eoeAndroid上首发的,但没有详细的实现说明:http://www.eoeandroid.com/thread-317901-1-1.html 在csdn上进行详细的说明吧.(同时上 ...
- linq中的group by
现有如下需求,要求统计int数组中每个整数的个数: ,,,,,,,,,,,, }; var linq = from item in arrInt group item by item into g// ...
- C# 方法的调用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- ecshop后台添加栏目
ecshop后台增加模块菜单详细教程 一:ecshop后台管理 admin\includes\inc_menu.php 添加上你要添加的功能admin\includes\inc_priv.php 对应 ...
- Struts1的实现原理
一 开文背景 -- 废话讲一段~ 本文借助动力节点-王勇老师的视频教程中的引例来了解struts1的实现原理,虽然现在已经很少使用struts1了,但是了解了其原理之后,对了解其他mvc框架还是有较大 ...
- CodeForces 385C Bear and Prime Numbers 素数打表
第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample h ...