命令模式(command pattern) 多命令 具体解释

本文地址: http://blog.csdn.net/caroline_wendy

參考命令模式: http://blog.csdn.net/caroline_wendy/article/details/31379977

具体步骤:

1. 多命令, 把未使用的命令, 初始化为空对象(NoCommand), 依据參数(slot), 选择输出命令.

/**
* @time 2014年6月16日
*/
package command; /**
* @author C.L.Wang
*
*/
public class RemoteControl { Command[] onCommands; //开
Command[] offCommands; //关 public RemoteControl() {
onCommands = new Command[7];
offCommands = new Command[7]; Command noCommand = new NoCommand(); for (int i=0; i<7; ++i) { //初始化
onCommands[i] = noCommand;
offCommands[i] = noCommand;
} } public void setCommand (int slot, Command onCommand, Command offCommand) {
this.onCommands[slot] = onCommand;
this.offCommands[slot] = offCommand;
} public void onButtonWasPushed(int slot) {
onCommands[slot].execute();
} public void offButtonWasPushed(int slot) {
offCommands[slot].execute();
} public String toString() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("\n------ Remote Control ------\n");
for (int i=0; i<onCommands.length; ++i) {
stringBuffer.append("[slot " + i + "] " + onCommands[i].getClass().getName()
+ " " + offCommands[i].getClass().getName() + "\n");
} return stringBuffer.toString();
}
}

2. 空命令(NoCommand)对象, 继承命令接口.

package command;

public class NoCommand implements Command {
public void execute() { }
}

3. 測试对象, 分别给多命令赋值(setCommand), 通过參数(slot)调用.

/**
* @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");
GarageDoor garageDoor = new GarageDoor("");
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); CeilingFanOnCommand ceilingFanOn = new CeilingFanOnCommand(ceilingFan);
CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan); GarageDoorOnCommand garageDoorOn = new GarageDoorOnCommand(garageDoor);
GarageDoorOffCommand garageDoorOff = new GarageDoorOffCommand(garageDoor); StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo);
StereoOffCommand stereoOffCommand = new StereoOffCommand(stereo); remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);
remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff);
remoteControl.setCommand(3, stereoOnWithCD, stereoOffCommand); System.out.println(remoteControl); remoteControl.onButtonWasPushed(0);
remoteControl.offButtonWasPushed(0);
remoteControl.onButtonWasPushed(1);
remoteControl.offButtonWasPushed(1);
remoteControl.onButtonWasPushed(2);
remoteControl.offButtonWasPushed(2);
remoteControl.onButtonWasPushed(3);
remoteControl.offButtonWasPushed(3);
} }

4. 输出:

------ Remote Control ------
[slot 0] command.LightOnCommand command.LightOffCommand
[slot 1] command.LightOnCommand command.LightOffCommand
[slot 2] command.CeilingFanOnCommand command.CeilingFanOffCommand
[slot 3] command.StereoOnWithCDCommand command.StereoOffCommand
[slot 4] command.NoCommand command.NoCommand
[slot 5] command.NoCommand command.NoCommand
[slot 6] command.NoCommand command.NoCommand Living Room Light is on
Living Room Light is off
Kitchen Light is on
Kitchen Light is off
Living Room ceiling fan is on high
Living Room ceiling fan is off
Living Room stereo is on
Living Room stereo is set for CD input
Living Room Stereo volume set to 11
Living Room stereo is off

其余代码下载: http://download.csdn.net/detail/u012515223/7506695

设计模式 - 命令模式(command pattern) 多命令 具体解释的更多相关文章

  1. 设计模式 - 命令模式(command pattern) 撤销(undo) 具体解释

    命令模式(command pattern) 撤销(undo) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.cs ...

  2. 乐在其中设计模式(C#) - 命令模式(Command Pattern)

    原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...

  3. 设计模式 - 命令模式(command pattern) 具体解释

    命令模式(command pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 命令模式(command pattern) : 将请求封装成对 ...

  4. 设计模式 - 命令模式(command pattern) 宏命令(macro command) 具体解释

    命令模式(command pattern) 宏命令(macro command) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考: 命名模式(撤销) ...

  5. 二十四种设计模式:命令模式(Command Pattern)

    命令模式(Command Pattern) 介绍将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可取消的操作. 示例有一个Message实体类,某个 ...

  6. 设计模式-15命令模式(Command Pattern)

    1.模式动机 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,我们只需在程序运行时指定具体的请求接收者即可,此时,可以使用命令模式来进行设计,使 ...

  7. 设计模式(六):控制台中的“命令模式”(Command Pattern)

    今天的博客中就来系统的整理一下“命令模式”.说到命令模式,我就想起了控制台(Console)中的命令.无论是Windows操作系统(cmd.exe)还是Linux操作系统(命令行式shell(Comm ...

  8. 命令模式(Command Pattern)

    命令模式属于对象的行为模式.命令模式又称为行动(Action)模式或交易(Transaction)模式.命令模式把一个请求或者操作封装到一个对象中.命令模式允许系统使用不同的请求把客户端参数化,对请求 ...

  9. 设计模式 - 组合模式(composite pattern) 迭代器(iterator) 具体解释

    组合模式(composite pattern) 迭代器(iterator) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考组合模式(composit ...

随机推荐

  1. BZOJ_1018_[SHOI2008]_交通堵塞traffic_(线段树)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1018 \(2*n\)的距形,起初没有边相连,之后有三种操作: 1.加边. 2.删边. 3.询问 ...

  2. [POJ 3370] Halloween treats

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7143   Accepted: 2641 ...

  3. apache开源项目--lume

    lume 是一个分布式.可靠和高可用的服务,用于收集.聚合以及移动大量日志数据,使用一个简单灵活的架构,就流数据模型.这是一个可靠.容错的服务.

  4. CentOS 7 安装 mariaDB

    1.安装数据库 [root@localhost ~]# yum -y  install mariadb-server mariadb mariadb-devel 2.启动数据库[root@localh ...

  5. C++学习笔记:List容器

    http://www.cplusplus.com/reference/list/list/ #include <list> list l:初始化一个0大小的表 list l(10):初始化 ...

  6. WCF 学习总结3 -- 实例模式

    通过WCF的ServiceBehaviorAttribute设定InstanceContextMode有下面的3中模式: 1. Single —— 表示所有的客户端共享一个会话(服务对象)(服务关闭时 ...

  7. java 垃圾回收(堆内存)、以及栈内存的释放

    一.Java的垃圾回收机制———解疑 Java的垃圾回收机制是Java虚拟机提供的能力,用于在空闲时间以不定时的方式动态回收无任何引用的对象占据的内存空间. 需要注意的是:垃圾回收回收的是无任何引用的 ...

  8. Mtom Encoding in WCF

    http://www.codeproject.com/Articles/632101/Mtom-Encoding-in-WCF http://msdn.microsoft.com/zh-cn/libr ...

  9. Myeclipse *.link用法

    引用路径 path=D:\\ProgramData\\MyEclipse\\adt

  10. 【Tcpcopy】离线回放功能

    最近因调试问题,需要一直进行tcpcopy,拿有问题的包进行测试.决定使用tcpcopy对录制脚本进行回放,以下为我操作的具体步骤.主要是三块 1 下载安装具有离线回放功能的tcpcopy 2 使用t ...