Command Pattern 命令模式
2015-07-06 05:52
417 查看
Command Pattern: encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations.
命令模式把一个请求或者操作封装到一个对象中。命令模式允许系统使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。
命令模式是对命令的封装。命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象。每一个命令都是一个操作:请求的一方发出请求要求执行一个操作;接收的一方收到请求,并执行操作。
Client: 生成Command,指定Invoker执行Command。
Command: 含有Receiver的引用,将Receiver的方法(一些方法)封装成一个execute()方法。成为一个Command Object。在某个Client产生,在某个Invoker执行。
Receiver: 一个普通的类。
Invoker: 执行Command的类,调用execute()方法。
1. The client creates a command object.
2. The client does a setCommand() to store the command object in the invoker.
3. The client asks the invoker to execute the command.
以遥控器控制灯为例:
定义Receiver:
定义命令接口:
定义Client:
如果命令包括更多的实现细节,则可把Receiver省略,生成一个self-contain的命令,内含execute()方法,便于Invoker调用。如Computer-Space模型下的Job,Task,Result。
A null object is useful when you don't have a meaningful object to return, and yet you want to remove the responsibility for handling
null from the client. Instead of declaring a reference to a null, you can declare it to an object which does nothing.
应用:Remote Control
命令模式把一个请求或者操作封装到一个对象中。命令模式允许系统使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。
命令模式是对命令的封装。命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象。每一个命令都是一个操作:请求的一方发出请求要求执行一个操作;接收的一方收到请求,并执行操作。
Client: 生成Command,指定Invoker执行Command。
Command: 含有Receiver的引用,将Receiver的方法(一些方法)封装成一个execute()方法。成为一个Command Object。在某个Client产生,在某个Invoker执行。
Receiver: 一个普通的类。
Invoker: 执行Command的类,调用execute()方法。
1. The client creates a command object.
2. The client does a setCommand() to store the command object in the invoker.
3. The client asks the invoker to execute the command.
以遥控器控制灯为例:
定义Receiver:
public class Light { public void on() { // light on } public void off() { // light off } }
定义命令接口:
public interface Command { public void execute(); public void undo(); }定义具体命令(Command):
public class LightOnCommand implements Command { private Light light; public LightOnCommand(Light light) { this.light = light; } @Override public void execute() { light.on(); } @Override public void undo() { light.off(); } } public class LightOffCommand implements Command { private Light light; public LightOffCommand(Light light) { this.light = light; } @Override public void execute() { light.off(); } @Override public void undo() { light.on(); } }定义Invoker:
public class RemoteControl { private Command onCommand; private Command offCommand; private Command undoCommand; public RemoteControl() { Command noCommand = new NoCommand(); onCommand = noCommand; offCommand = noCommand; } public void setCommmand(Command onCommand, Command offCommand) { this.onCommand = onCommand; this.offCommand = offCommand; } public void onButtonPushed() { onCommand.execute(); undoCommand = onCommand; } public void offButtonPushed() { offCommand.execute(); undoCommand = offCommand; } }
定义Client:
public class RemoteLoader { public static void main(String[] args) { Light light = new Light(); LightOnCommand lightON = new LightOnCommand(light); LightOffCommand lightOFF = new LightOffCommand(light); RemoteControl control = new RemoteControl(); control.setCommmand(lightON, lightOFF); control.onButtonPushed(); control.offButtonPushed(); } }
如果命令包括更多的实现细节,则可把Receiver省略,生成一个self-contain的命令,内含execute()方法,便于Invoker调用。如Computer-Space模型下的Job,Task,Result。
A null object is useful when you don't have a meaningful object to return, and yet you want to remove the responsibility for handling
null from the client. Instead of declaring a reference to a null, you can declare it to an object which does nothing.
应用:Remote Control
相关文章推荐
- 使用InstallUtil对Windows服务进行安装与卸载
- error C2440: 'static_cast' : cannot convert from 'void (__thiscall CChatDlg::* )(WPARAM,LPARAM)' to
- cannot open Debug/*.exe for writing
- [Leetcode][JAVA] Binary Tree Level Order Traversal
- [LeetCode] Power of Two 判断2的次方数
- Matlab应用实例(4)—linprog(02高级)
- ActionBar Fragment运用最佳实践
- IDNLoopView 图片/视图循环播放控件
- 摘录-IT企业必读的200个.NET面试题-09 .NET中的数据库开发
- 关于OC的内存管理-01
- hdu 4989 Summary
- 调多线程中的消息发送
- Implement Trie (Prefix Tree)
- .Net常见错误
- [C语言]进阶|结构类型: 枚举, 结构, 类型定义
- hdu 5146 Sequence
- SQLServer学习笔记系列10
- hdu 5182 PM2.5
- 电脑光标总是失去焦点解决方法
- Java的swing编程(一)