🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 命令模式(Command) > 将一个请求封装为一个对象,使发出请求的责任和执行请求的责任分割开。行为请求者与行为实现者解耦。就像军队里的“敬礼”,不管是谁听到 这个命令都会做出标准的敬礼动作。这样两者之间通过命令对象进行沟通,这样方便将命令对象进行储存、传递、调用、增加与管理 **主要优点:** 1. 降低系统的耦合度。命令模式能将调用操作的对象与实现该操作的对象解耦。 2. 增加或删除命令非常方便。采用命令模式增加与删除命令不会影响其他类,它满足“开闭原则”,对扩展比较灵活。便于添加和修改行为,便于聚合多个命令。 3. 可以实现宏命令。命令模式可以与[组合模式](http://c.biancheng.net/view/1373.html)结合,将多个命令装配成一个组合命令,即宏命令。 4. 方便实现 Undo 和 Redo 操作。命令模式可以与后面介绍的[备忘录模式](http://c.biancheng.net/view/1400.html)结合,实现命令的撤销与恢复。 **缺点:** - 可能产生大量具体命令类。因为计对每一个具体操作都需要设计一个具体命令类,这将增加系统的复杂性。 ## **命令模式的应用场景** 1. 当系统需要将请求调用者与请求接收者解耦时,命令模式使得调用者和接收者不直接交互。 2. 当系统需要随机请求命令或经常增加或删除命令时,命令模式比较方便实现这些功能。 3. 当系统需要执行一组操作时,命令模式可以定义宏命令来实现该功能。 4. 当系统需要支持命令的撤销(Undo)操作和恢复(Redo)操作时,可以将命令对象存储起来,采用备忘录模式来实现。 在软件系统中,“行为请求者”与“行为实现者”通常呈现一种“紧耦合”。但在某些场合,比如要对行为进行“记录、撤销/重做、事务”等处理,这种无法抵御变化的紧耦合是不合适的。在这种情况下,如何将“行为请求者”与“行为实现者”解耦?将一组行为抽象为对象,实现二者之间的松耦合。这就是命令模式。 ### **模式的结构** 1. 抽象命令类(Command)角色:声明执行命令的接口,拥有执行命令的抽象方法 execute()。 2. 具体命令角色(Concrete Command)角色:是抽象命令类的具体实现类,它拥有接收者对象,并通过调用接收者的功能来完成命令要执行的操作。 3. 实现者/接收者(Receiver)角色:执行命令功能的相关操作,是具体命令对象业务的真正实现者。 4. 调用者/请求者(Invoker)角色:是请求的发送者,它通常拥有很多的命令对象,并通过访问命令对象来执行相关请求,它不直接访问接收者 ![](https://img.kancloud.cn/0f/98/0f985cdc946c92ea9f54270fd9632328_567x630.png) ``` <pre class="calibre10">``` <span class="token">//调用者 接收具体命令类并调用具体命令类的执行方法</span> class <span class="token4">Invoker</span> <span class="token3">{</span> private $command<span class="token3">;</span> public <span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span>Command $command<span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>command<span class="token1">=</span>$command<span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">setCommand</span><span class="token3">(</span>Command $command<span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>command<span class="token1">=</span>$command<span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">call</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token4">print_r</span><span class="token3">(</span><span class="token2">"调用者执行命令command...<br>"</span><span class="token3">)</span><span class="token3">;</span> $this<span class="token1">-</span><span class="token1">></span>command<span class="token1">-</span><span class="token1">></span><span class="token4">execute</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">//抽象命令</span> interface <span class="token4">Command</span> <span class="token3">{</span> public <span class="token5">function</span> <span class="token4">execute</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token">//具体命令 作用是接收接受者并执行接受者的action命令</span> class <span class="token4">ConcreteCommand</span> implements <span class="token4">Command</span> <span class="token3">{</span> private $receiver<span class="token3">;</span> public <span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>receiver <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Receiver</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">execute</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>receiver<span class="token1">-</span><span class="token1">></span><span class="token4">action</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">//接收者</span> class <span class="token4">Receiver</span> <span class="token3">{</span> public <span class="token5">function</span> <span class="token4">action</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token4">print_r</span><span class="token3">(</span><span class="token2">"接收者的action()方法被调用...<br>"</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> class <span class="token4">Client</span> <span class="token3">{</span> public static <span class="token5">function</span> <span class="token4">main</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> $command<span class="token1">=</span><span class="token5">new</span> <span class="token4">ConcreteCommand</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $invoker<span class="token1">=</span><span class="token5">new</span> <span class="token4">Invoker</span><span class="token3">(</span>$command<span class="token3">)</span><span class="token3">;</span> <span class="token4">print_r</span><span class="token3">(</span><span class="token2">"客户访问调用者的call()方法...<br>"</span><span class="token3">)</span><span class="token3">;</span> $invoker<span class="token1">-</span><span class="token1">></span><span class="token4">call</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> Client<span class="token3">:</span><span class="token3">:</span><span class="token4">main</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> ``` ``` 有两种基本的思路来实现可撤销的操作: 1、逆向操作实现:比如被撤销的操作是添加功能,那撤消的实现就变成删除功能;同理被撤销的操作是删除功能,那么撤销的实现就变成添加功能; 2、存储恢复实现,意思就是把操作前的状态记录下来,然后要撤销操作的时候就直接恢复回去就可以了,可使用备忘录模式(Memento Pattern)来实现(稍后会讲到备忘录模式); ### **逆向操作来实现撤销(undo):** ``` <pre class="calibre10">``` <span class="token">/** * 包含撤销命令的接口 */</span> interface <span class="token4">Command</span> <span class="token3">{</span> <span class="token">//执行方法</span> public <span class="token5">function</span> <span class="token4">execute</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//撤销方法</span> public <span class="token5">function</span> <span class="token4">undo</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token">/** * 具体命令 -- 创建目录 */</span> class <span class="token4">CreateDirCommand</span> implements <span class="token4">Command</span> <span class="token3">{</span> private $dirName<span class="token3">;</span><span class="token">//string</span> public $makeDir<span class="token3">;</span> public <span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span>MakeDir $makeDir<span class="token3">,</span>String $dirName<span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>makeDir <span class="token1">=</span> $makeDir<span class="token3">;</span> $this<span class="token1">-</span><span class="token1">></span>dirName <span class="token1">=</span> $dirName<span class="token3">;</span> <span class="token3">}</span> <span class="token">/** * 逆向操作实现:比如被撤销的操作是添加功能,那撤消的实现就变成删除功能;同理被撤销的操作是删除功能,那么撤销的实现就变成添加功能; * @Override */</span> public <span class="token5">function</span> <span class="token4">execute</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>makeDir<span class="token1">-</span><span class="token1">></span><span class="token4">createDir</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>dirName<span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token">/** * 逆向操作实现:比如被撤销的操作是添加功能,那撤消的实现就变成删除功能;同理被撤销的操作是删除功能,那么撤销的实现就变成添加功能; * @Override */</span> public <span class="token5">function</span> <span class="token4">undo</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>makeDir<span class="token1">-</span><span class="token1">></span><span class="token4">deleteDir</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>dirName<span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">/** * 命令接受者 * 包含两个命令:创建目录、删除目录 */</span> class <span class="token4">MakeDir</span> <span class="token3">{</span> <span class="token">//创建目录</span> public <span class="token5">function</span> <span class="token4">createDir</span><span class="token3">(</span>String $filename<span class="token3">)</span><span class="token3">{</span> <span class="token5">if</span> <span class="token3">(</span><span class="token4">file_exists</span><span class="token3">(</span>$filename<span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span> <span class="token4">print_r</span><span class="token3">(</span><span class="token2">"创建目录 "</span> <span class="token3">.</span> $filename <span class="token3">.</span> <span class="token2">" 失败,目标目录已经存在"</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span><span class="token5">else</span><span class="token3">{</span> <span class="token">//创建目录 </span> <span class="token5">if</span> <span class="token3">(</span><span class="token4">mkdir</span><span class="token3">(</span>$filename<span class="token3">,</span><span class="token6">0777</span><span class="token3">,</span><span class="token6">true</span><span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span> <span class="token4">print_r</span><span class="token3">(</span><span class="token2">"创建目录 "</span> <span class="token3">.</span> $filename <span class="token3">.</span> <span class="token2">" 成功"</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token5">else</span> <span class="token3">{</span> <span class="token4">print_r</span><span class="token3">(</span><span class="token2">"创建目录 "</span> <span class="token3">.</span> $filename <span class="token3">.</span> <span class="token2">" 失败"</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">//删除目录</span> public <span class="token5">function</span> <span class="token4">deleteDir</span><span class="token3">(</span>String $filename<span class="token3">)</span><span class="token3">{</span> <span class="token5">if</span><span class="token3">(</span><span class="token4">file_exists</span><span class="token3">(</span>$filename<span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span> <span class="token5">if</span><span class="token3">(</span><span class="token4">rmdir</span><span class="token3">(</span>$filename<span class="token3">)</span><span class="token3">)</span><span class="token3">{</span> <span class="token4">print_r</span><span class="token3">(</span><span class="token2">"删除目录 "</span> <span class="token3">.</span> $filename <span class="token3">.</span> <span class="token2">" 成功"</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span><span class="token5">else</span><span class="token3">{</span> <span class="token4">print_r</span><span class="token3">(</span><span class="token2">"删除目录 "</span> <span class="token3">.</span> $filename <span class="token3">.</span> <span class="token2">" 失败"</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span><span class="token5">else</span><span class="token3">{</span> <span class="token4">print_r</span><span class="token3">(</span><span class="token2">"删除目录 "</span> <span class="token3">.</span> $filename <span class="token3">.</span> <span class="token2">" 失败,目标目录不存在"</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">/** * 请求者 */</span> class <span class="token4">RequestMakeDir</span> <span class="token3">{</span> public $createCommand<span class="token3">;</span> public <span class="token5">function</span> <span class="token4">setCreateCommand</span><span class="token3">(</span>Command $command<span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>createCommand <span class="token1">=</span> $command<span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">executeCreateCommand</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>createCommand<span class="token1">-</span><span class="token1">></span><span class="token4">execute</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">undoCreateCommand</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>createCommand<span class="token1">-</span><span class="token1">></span><span class="token4">undo</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">/** * 测试 */</span> class <span class="token4">Client</span> <span class="token3">{</span> public static <span class="token5">function</span> <span class="token4">main</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> $dir <span class="token1">=</span> <span class="token2">"E:\\command2017"</span><span class="token3">;</span> <span class="token">//创建接收者</span> $makeDir <span class="token1">=</span> <span class="token5">new</span> <span class="token4">MakeDir</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//创建具体命令并且指定接收者</span> $create_command <span class="token1">=</span> <span class="token5">new</span> <span class="token4">CreateDirCommand</span><span class="token3">(</span>$makeDir<span class="token3">,</span>$dir<span class="token3">)</span><span class="token3">;</span> <span class="token">//创建请求者</span> $requestMakeDir <span class="token1">=</span> <span class="token5">new</span> <span class="token4">RequestMakeDir</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//设置命令</span> $requestMakeDir<span class="token1">-</span><span class="token1">></span><span class="token4">setCreateCommand</span><span class="token3">(</span>$create_command<span class="token3">)</span><span class="token3">;</span> <span class="token">/***********创建目录及撤销**************/</span> <span class="token">//创建目录</span> $requestMakeDir<span class="token1">-</span><span class="token1">></span><span class="token4">executeCreateCommand</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//撤销</span> $requestMakeDir<span class="token1">-</span><span class="token1">></span><span class="token4">undoCreateCommand</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> Client<span class="token3">:</span><span class="token3">:</span><span class="token4">main</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> ``` ``` 多次撤销 ``` <pre class="calibre10">``` <span class="token">/** * 包含撤销命令的接口 */</span> interface <span class="token4">Command</span> <span class="token3">{</span> <span class="token">//执行方法</span> public <span class="token5">function</span> <span class="token4">execute</span><span class="token3">(</span>String $dirName<span class="token3">)</span><span class="token3">;</span> <span class="token">//撤销方法</span> public <span class="token5">function</span> <span class="token4">undo</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token">/** * 具体命令 -- 创建目录 */</span> class <span class="token4">CreateDirCommand</span> implements <span class="token4">Command</span> <span class="token3">{</span> private $dirNameList<span class="token1">=</span><span class="token4">array</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span><span class="token">//array</span> public $makeDir<span class="token3">;</span> public <span class="token5">function</span> <span class="token4">__construct</span><span class="token3">(</span>MakeDir $makeDir<span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>makeDir <span class="token1">=</span> $makeDir<span class="token3">;</span> <span class="token3">}</span> <span class="token">/** * 逆向操作实现:比如被撤销的操作是添加功能,那撤消的实现就变成删除功能;同理被撤销的操作是删除功能,那么撤销的实现就变成添加功能; * @Override */</span> public <span class="token5">function</span> <span class="token4">execute</span><span class="token3">(</span>String $dirName<span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>makeDir<span class="token1">-</span><span class="token1">></span><span class="token4">createDir</span><span class="token3">(</span>$dirName<span class="token3">)</span><span class="token3">;</span> $this<span class="token1">-</span><span class="token1">></span>dirNameList<span class="token3">[</span><span class="token3">]</span><span class="token1">=</span>$dirName<span class="token3">;</span> <span class="token3">}</span> <span class="token">/** * 逆向操作实现:比如被撤销的操作是添加功能,那撤消的实现就变成删除功能;同理被撤销的操作是删除功能,那么撤销的实现就变成添加功能; * @Override */</span> public <span class="token5">function</span> <span class="token4">undo</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token5">if</span> <span class="token3">(</span><span class="token4">count</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>dirNameList<span class="token3">)</span><span class="token1">></span><span class="token6">0</span><span class="token3">)</span> <span class="token3">{</span> <span class="token">//获取数组中最后一个文件</span> $lastFile<span class="token1">=</span>$this<span class="token1">-</span><span class="token1">></span>dirNameList<span class="token3">[</span><span class="token4">count</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>dirNameList<span class="token3">)</span><span class="token1">-</span><span class="token6">1</span><span class="token3">]</span><span class="token3">;</span> $this<span class="token1">-</span><span class="token1">></span>makeDir<span class="token1">-</span><span class="token1">></span><span class="token4">deleteDir</span><span class="token3">(</span>$lastFile<span class="token3">)</span><span class="token3">;</span> <span class="token">//删除成功后将存放的文件移除</span> <span class="token4">array_pop</span><span class="token3">(</span>$this<span class="token1">-</span><span class="token1">></span>dirNameList<span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span><span class="token5">else</span><span class="token3">{</span> echo <span class="token2">"没有需要撤销的操作!"</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">/** * 命令接受者 * 包含两个命令:创建目录、删除目录 */</span> class <span class="token4">MakeDir</span> <span class="token3">{</span> <span class="token">//创建目录</span> public <span class="token5">function</span> <span class="token4">createDir</span><span class="token3">(</span>String $dirname<span class="token3">)</span><span class="token3">{</span> <span class="token5">if</span> <span class="token3">(</span><span class="token4">file_exists</span><span class="token3">(</span>$dirname<span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span> <span class="token4">print_r</span><span class="token3">(</span><span class="token2">"创建目录 "</span> <span class="token3">.</span> $dirname <span class="token3">.</span> <span class="token2">" 失败,目标目录已经存在"</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span><span class="token5">else</span><span class="token3">{</span> <span class="token">//创建目录 </span> <span class="token5">if</span> <span class="token3">(</span><span class="token4">mkdir</span><span class="token3">(</span>$dirname<span class="token3">,</span><span class="token6">0777</span><span class="token3">,</span><span class="token6">true</span><span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span> <span class="token4">print_r</span><span class="token3">(</span><span class="token2">"创建目录 "</span> <span class="token3">.</span> $dirname <span class="token3">.</span> <span class="token2">" 成功"</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token5">else</span> <span class="token3">{</span> <span class="token4">print_r</span><span class="token3">(</span><span class="token2">"创建目录 "</span> <span class="token3">.</span> $dirname <span class="token3">.</span> <span class="token2">" 失败"</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">//删除目录</span> public <span class="token5">function</span> <span class="token4">deleteDir</span><span class="token3">(</span>String $dirname<span class="token3">)</span><span class="token3">{</span> <span class="token5">if</span><span class="token3">(</span><span class="token4">file_exists</span><span class="token3">(</span>$dirname<span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span> <span class="token5">if</span><span class="token3">(</span><span class="token4">rmdir</span><span class="token3">(</span>$dirname<span class="token3">)</span><span class="token3">)</span><span class="token3">{</span> <span class="token4">print_r</span><span class="token3">(</span><span class="token2">"删除目录 "</span> <span class="token3">.</span> $dirname <span class="token3">.</span> <span class="token2">" 成功"</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span><span class="token5">else</span><span class="token3">{</span> <span class="token4">print_r</span><span class="token3">(</span><span class="token2">"删除目录 "</span> <span class="token3">.</span> $dirname <span class="token3">.</span> <span class="token2">" 失败"</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span><span class="token5">else</span><span class="token3">{</span> <span class="token4">print_r</span><span class="token3">(</span><span class="token2">"删除目录 "</span> <span class="token3">.</span> $dirname <span class="token3">.</span> <span class="token2">" 失败,目标目录不存在"</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">/** * 请求者 */</span> class <span class="token4">RequestMakeDir</span> <span class="token3">{</span> public $createCommand<span class="token3">;</span> public <span class="token5">function</span> <span class="token4">setCreateCommand</span><span class="token3">(</span>Command $command<span class="token3">)</span> <span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>createCommand <span class="token1">=</span> $command<span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">executeCreateCommand</span><span class="token3">(</span>String $dirname<span class="token3">)</span><span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>createCommand<span class="token1">-</span><span class="token1">></span><span class="token4">execute</span><span class="token3">(</span>$dirname<span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">undoCreateCommand</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> $this<span class="token1">-</span><span class="token1">></span>createCommand<span class="token1">-</span><span class="token1">></span><span class="token4">undo</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">/** * 测试 */</span> class <span class="token4">Client</span> <span class="token3">{</span> public static <span class="token5">function</span> <span class="token4">main</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token">//创建接收者</span> $makeDir <span class="token1">=</span> <span class="token5">new</span> <span class="token4">MakeDir</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//创建具体命令并且指定接收者</span> $create_command <span class="token1">=</span> <span class="token5">new</span> <span class="token4">CreateDirCommand</span><span class="token3">(</span>$makeDir<span class="token3">)</span><span class="token3">;</span> <span class="token">//创建请求者</span> $requestMakeDir <span class="token1">=</span> <span class="token5">new</span> <span class="token4">RequestMakeDir</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//设置命令</span> $requestMakeDir<span class="token1">-</span><span class="token1">></span><span class="token4">setCreateCommand</span><span class="token3">(</span>$create_command<span class="token3">)</span><span class="token3">;</span> <span class="token">/***********创建目录及撤销**************/</span> <span class="token">//创建目录</span> $requestMakeDir<span class="token1">-</span><span class="token1">></span><span class="token4">executeCreateCommand</span><span class="token3">(</span><span class="token2">"E:\\command2017"</span><span class="token3">)</span><span class="token3">;</span> $requestMakeDir<span class="token1">-</span><span class="token1">></span><span class="token4">executeCreateCommand</span><span class="token3">(</span><span class="token2">"E:\\command2018"</span><span class="token3">)</span><span class="token3">;</span> $requestMakeDir<span class="token1">-</span><span class="token1">></span><span class="token4">executeCreateCommand</span><span class="token3">(</span><span class="token2">"E:\\command2019"</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//多次撤销</span> $requestMakeDir<span class="token1">-</span><span class="token1">></span><span class="token4">undoCreateCommand</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $requestMakeDir<span class="token1">-</span><span class="token1">></span><span class="token4">undoCreateCommand</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $requestMakeDir<span class="token1">-</span><span class="token1">></span><span class="token4">undoCreateCommand</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $requestMakeDir<span class="token1">-</span><span class="token1">></span><span class="token4">undoCreateCommand</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $requestMakeDir<span class="token1">-</span><span class="token1">></span><span class="token4">undoCreateCommand</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> Client<span class="token3">:</span><span class="token3">:</span><span class="token4">main</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> 输出: 创建目录 E<span class="token3">:</span>\\command2017 成功创建目录 E<span class="token3">:</span>\\command2018 成功创建目录 E<span class="token3">:</span>\\command2019 成功删除目录 E<span class="token3">:</span>\\command2019 成功删除目录 E<span class="token3">:</span>\\command2018 成功删除目录 E<span class="token3">:</span>\\command2017 成功没有需要撤销的操作!没有需要撤销的操作! ``` ``` ## 命令模式的扩展 在软件开发中,有时将命令模式与前面学的组合模式联合使用,这就构成了宏命令模式,也叫组合命令模式。宏命令包含了一组命令,它充当了具体命令与调用者的双重角色,执行它时将递归调用它所包含的所有命令,其具体结构图如图 。 ![](https://img.kancloud.cn/f5/0d/f50da3687a58ea013b5826d06114b761_774x486.png)