命令模式(Command Pattern)
——.NET设计模式系列之十七
TerryLee
,2006年7月
概述
在软件系统中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”。但在某些场合,比如要对行为进行“记录、撤销/重做、事务”等处理,这种无法抵御变化的紧耦合是不合适的。在这种情况下,如何将“行为请求者”与“行为实现者”解耦?将一组行为抽象为对象,可以实现二者之间的松耦合[李建忠]。这就是本文要说的Command模式。
意图
将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作。[GOF 《设计模式》]
结构图
Command
模式结构图如下:
![](http://terrylee.blog.51cto.com/attachment/200803/200803261206500692046.jpg?x-oss-process=image/resize,w_1400/format,webp)
生活中的例子
Command
模式将一个请求封装为一个对象,从而使你可以使用不同的请求对客户进行参数化。用餐时的账单是Command模式的一个例子。服务员接受顾客的点单,把它记在账单上封装。这个点单被排队等待烹饪。注意这里的"账单"是不依赖于菜单的,它可以被不同的顾客使用,因此它可以添入不同的点单项目。
![](http://terrylee.blog.51cto.com/attachment/200803/200803261206500701031.jpg?x-oss-process=image/resize,w_1400/format,webp)
Command
模式解说
在众多的设计模式中,Command模式是很简单也很优雅的一种设计模式。Command模式它封装的是命令,把命令发出者的责任和命令执行者的责任分开。我们知道,一个类是一组操作和相应的一些变量的集合,现在有这样一个类
Document
,如下:
图3
![](http://terrylee.blog.51cto.com/attachment/200803/200803261206500711562.jpg?x-oss-process=image/resize,w_1400/format,webp)
示意性代码:
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![dot.gif](http://terrylee.cnblogs.com/Images/dot.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![dot.gif](http://terrylee.cnblogs.com/Images/dot.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![dot.gif](http://terrylee.cnblogs.com/Images/dot.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
一般情况下我们使用这个类的时候,都会这样去写:
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
这样的使用本来是没有任何问题的,但是我们看到在这个特定的应用中,出现了
Undo/Redo的操作,这时如果行为的请求者和行为的实现者之间还是呈现这样一种紧耦合,就不太合适了。可以看到,客户程序是依赖于具体
Document的命令(方法)的,引入
Command模式,需要对
Document中的三个命令进行抽象,这是
Command模式最有意思的地方,因为在我们看来
Display()
,Undo(),Redo()这三个方法都应该是Document所具有的,如果单独抽象出来成一个命令对象,那就是把函数层面的功能提到了类的层面,有点功能分解的味道,我觉得这正是Command模式解决这类问题的优雅之处,先对命令对象进行抽象:
![](http://terrylee.blog.51cto.com/attachment/200803/200803261206500721875.jpg?x-oss-process=image/resize,w_1400/format,webp)
示意性代码:
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
其他的具体命令类都继承于该抽象类,如下:
![](http://terrylee.blog.51cto.com/attachment/200803/200803261206500731718.jpg?x-oss-process=image/resize,w_1400/format,webp)
图5
示意性代码:
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
现在还需要一个
Invoker角色的类,这其实相当于一个中间角色,前面我曾经说过,使用这样的一个中间层也是我们经常使用的手法,即把
A对
B的依赖转换为
A对
C的依赖。如下:
![](http://terrylee.blog.51cto.com/attachment/200803/200803261206500742234.jpg?x-oss-process=image/resize,w_1400/format,webp)
示意性代码:
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
现在再来看客户程序的调用代码:
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
可以看到:
1
.在客户程序中,不再依赖于Document的Display(),Undo(),Redo()命令,通过Command对这些命令进行了封装,使用它的一个关键就是抽象的Command类,它定义了一个操作的接口。同时我们也可以看到,本来这三个命令仅仅是三个方法而已,但是通过Command模式却把它们提到了类的层面,这其实是违背了面向对象的原则,但它却优雅的解决了分离命令的请求者和命令的执行者的问题,在使用Command模式的时候,一定要判断好使用它的时机。
2
.上面的Undo/Redo只是简单示意性的实现,如果要实现这样的效果,需要对命令对象设置一个状态,由命令对象可以把状态存储起来。
.NET
中的Command模式
在ASP.NET的MVC模式中,有一种叫Front Controller的模式,它分为Handler和Command树两个部分,Handler处理所有公共的逻辑,接收HTTP Post或Get请求以及相关的参数并根据输入的参数选择正确的命令对象,然后将控制权传递到Command对象,由其完成后面的操作,这里面其实就是用到了Command模式。
![](http://terrylee.blog.51cto.com/attachment/200803/200803261206500758234.jpg?x-oss-process=image/resize,w_1400/format,webp)
![](http://terrylee.blog.51cto.com/attachment/200803/200803261206500772312.jpg?x-oss-process=image/resize,w_1400/format,webp)
Handler
类负责处理各个 Web 请求,并将确定正确的 Command 对象这一职责委派给 CommandFactory 类。当 CommandFactory 返回 Command 对象后,Handler 将调用 Command 上的 Execute 方法来执行请求。具体的实现如下
Handler
类:
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
Command
接口:
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
CommandFactory
类:
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
RedirectCommand
类:
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
FirstPortal
类:
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
SecondPortal
类:
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![None.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/None.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockStart.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif?x-oss-process=image/resize,w_1400/format,webp)
![InBlock.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/InBlock.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedSubBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
![ExpandedBlockEnd.gif](http://terrylee.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif?x-oss-process=image/resize,w_1400/format,webp)
效果及实现要点
1
.Command模式的根本目的在于将“行为请求者”与“行为实现者”解耦,在面向对象语言中,常见的实现手段是“将行为抽象为对象”。
2
.实现Command接口的具体命令对象ConcreteCommand有时候根据需要可能会保存一些额外的状态信息。
3
.通过使用Compmosite模式,可以将多个命令封装为一个“复合命令”MacroCommand。
4
.Command模式与C#中的Delegate有些类似。但两者定义行为接口的规范有所区别:Command以面向对象中的“接口-实现”来定义行为接口规范,更严格,更符合抽象原则;Delegate以函数签名来定义行为接口规范,更灵活,但抽象能力比较弱。
5
.使用命令模式会导致某些系统有过多的具体命令类。某些系统可能需要几十个,几百个甚至几千个具体命令类,这会使命令模式在这样的系统里变得不实际。
适用性
在下面的情况下应当考虑使用命令模式:
1
.使用命令模式作为"CallBack"在面向对象系统中的替代。"CallBack"讲的便是先将一个函数登记上,然后在以后调用此函数。
2
.需要在不同的时间指定请求、将请求排队。一个命令对象和原先的请求发出者可以有不同的生命期。换言之,原先的请求发出者可能已经不在了,而命令对象本身仍然是活动的。这时命令的接收者可以是在本地,也可以在网络的另外一个地址。命令对象可以在串形化之后传送到另外一台机器上去。
3
.系统需要支持命令的撤消(undo)。命令对象可以把状态存储起来,等到客户端需要撤销命令所产生的效果时,可以调用undo()方法,把命令所产生的效果撤销掉。命令对象还可以提供redo()方法,以供客户端在需要时,再重新实施命令效果。
4
.如果一个系统要将系统中所有的数据更新到日志里,以便在系统崩溃时,可以根据日志里读回所有的数据更新命令,重新调用Execute()方法一条一条执行这些命令,从而恢复系统在崩溃前所做的数据更新。
总结
Command
模式是非常简单而又优雅的一种设计模式,它的根本目的在于将“行为请求者”与“行为实现者”解耦。
更多的设计模式文章可以访问《.NET设计模式系列文章》
参考资料
Erich Gamma
等,《设计模式:可复用面向对象软件的基础》,机械工业出版社
Robert C.Martin
,《敏捷软件开发:原则、模式与实践》,清华大学出版社
阎宏,《Java与模式》,电子工业出版社
Alan Shalloway James R. Trott
,《Design Patterns Explained》,中国电力出版社
MSDN WebCast
《C#面向对象设计模式纵横谈(14):Command命令模式(结构型模式)》
袁剑,《领悟Web设计模式》
本文转自lihuijun51CTO博客,原文链接:
http://blog.51cto.com/terrylee/67767
,如需转载请自行联系原作者