当前位置:Gxlcms > PHP教程 > 学习php设计模式 php实现命令模式(command)

学习php设计模式 php实现命令模式(command)

时间:2021-07-01 10:21:17 帮助过:8人阅读

一、意图
将一个请求封装为一个对象,从而使用你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤消的操作。
可变的方面是:何时,怎样满足一个请求
命令模式是对命令的封装。命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象。
请求的一方发出请求要求执行一个操作;接收的一方收到请求,并执行操作。命令模式允许请求的一方和接收的一方独立开来,使得请求的一方不必知道接收请求的一方的接口,更不必知道请求是怎么被接收,以及操作是否被执行、何时被执行,以及是怎么被执行的。
二、命令模式结构图

三、命令模式中主要角色
命令(Command)角色:声明了一个给所有具体命令类的抽象接口。这是一个抽象角色。
具体命令(ConcreteCommand)角色:定义一个接受者和行为之间的弱耦合;实现Execute()方法,负责调用接收考的相应操作。Execute()方法通常叫做执行方法。
客户(Client)角色:创建了一个具体命令(ConcreteCommand)对象并确定其接收者。
请求者(Invoker)角色:负责调用命令对象执行请求,相关的方法叫做行动方法。
接收者(Receiver)角色:负责具体实施和执行一个请求。任何一个类都可以成为接收者,实施和执行请求的方法叫做行动方法。
四、命令模式的优点
命令模式的优点:
1、命令模式把请求一个操作的对象与知道怎么执行一个操作的对象分离开。
2、命令类与其他任何别的类一样,可以修改和推广。
3、可以把命令对象聚合在一起,合成为合成命令。
4、可以很容易的加入新的命令类。
命令模式的缺点:可能会导致某些系统有过多的具体命令类。
五、命令模式适用场景
1、抽象出待执行的动作以参数化对象。Command模式是回调机制的一个面向对象的替代品。
2、在不同的时刻指定、排列和执行请求。
3、支持取消操作。
4、支持修改日志。
5、用构建在原语操作上的高层操作构造一个系统。Command模式提供了对事务进行建模的方法。Command有一个公共的接口,使得你可以用同一种方式调用所有的事务。同时使用该模式也易于添加新事务以扩展系统。
六、命令模式与其它模式
合成模式(composite模式):Composite模式可被实现宏命令
原型模式(prototype模式):如果命令类带有clone(或在之前的文章中提到的copy方法)方法,命令就可以被复制。在命令模式支持多次取消操作时可能需要用到此模式,以复制当前状态的Command对象
七、命令模式PHP示例

  1. <?php
  2. /**
  3. * 命令角色
  4. */
  5. interface Command {
  6. /**
  7. * 执行方法
  8. */
  9. public function execute();
  10. }
  11. /**
  12. * 具体命令角色
  13. */
  14. class ConcreteCommand implements Command {
  15. private $_receiver;
  16. public function __construct(Receiver $receiver) {
  17. $this->_receiver = $receiver;
  18. }
  19. /**
  20. * 执行方法
  21. */
  22. public function execute() {
  23. $this->_receiver->action();
  24. }
  25. }
  26. /**
  27. * 接收者角色
  28. */
  29. class Receiver {
  30. /* 接收者名称 */
  31. private $_name;
  32. public function __construct($name) {
  33. $this->_name = $name;
  34. }
  35. /**
  36. * 行动方法
  37. */
  38. public function action() {
  39. echo $this->_name, ' do action.<br />';
  40. }
  41. }
  42. /**
  43. * 请求者角色
  44. */
  45. class Invoker {
  46. private $_command;
  47. public function __construct(Command $command) {
  48. $this->_command = $command;
  49. }
  50. public function action() {
  51. $this->_command->execute();
  52. }
  53. }
  54. /**
  55. * 客户端
  56. */
  57. class Client {
  58. /**
  59. * Main program.
  60. */
  61. public static function main() {
  62. $receiver = new Receiver('phpppan');
  63. $command = new ConcreteCommand($receiver);
  64. $invoker = new Invoker($command);
  65. $invoker->action();
  66. }
  67. }
  68. Client::main();
  69. ?>


八、命令模式协作
1、Client创建一个ConcreteCommand对象并指定它的Receiver对象
2、某Invoker对象存储该ConcreteCommand对象
3、该Invoker通过调用Command对象的execute操作来提交一个请求。若该命令是可撤消的,ConcreteCommand就在执行execute操作之前存储当前状态以用于取消命令。
4、ConcreteCommand对象对调用它的Receiver的一些操作以执行该请求。
九、宏命令
在这里,我们以一个简单的增加和粘贴功能为例,将这两个命令组成一个宏命令。
我们可以新建复制命令和粘贴命令,然后将其添加到宏命令中去。
如下所示代码:

  1. <?php
  2. /**
  3. * 命令角色
  4. */
  5. interface Command {
  6. /**
  7. * 执行方法
  8. */
  9. public function execute();
  10. }
  11. /**
  12. * 宏命令接口
  13. */
  14. interface MacroCommand extends Command {
  15. /**
  16. * 宏命令聚集的管理方法,可以删除一个成员命令
  17. * @param Command $command
  18. */
  19. public function remove(Command $command);
  20. /**
  21. * 宏命令聚集的管理方法,可以增加一个成员命令
  22. * @param Command $command
  23. */
  24. public function add(Command $command);
  25. }
  26. class DemoMacroCommand implements MacroCommand {
  27. private $_commandList;
  28. public function __construct() {
  29. $this->_commandList = array();
  30. }
  31. public function remove(Command $command) {
  32. $key = array_search($command, $this->_commandList);
  33. if ($key === FALSE) {
  34. return FALSE;
  35. }
  36. unset($this->_commandList[$key]);
  37. return TRUE;
  38. }
  39. public function add(Command $command) {
  40. return array_push($this->_commandList, $command);
  41. }
  42. public function execute() {
  43. foreach ($this->_commandList as $command) {
  44. $command->execute();
  45. }
  46. }
  47. }
  48. /**
  49. * 具体拷贝命令角色
  50. */
  51. class CopyCommand implements Command {
  52. private $_receiver;
  53. public function __construct(Receiver $receiver) {
  54. $this->_receiver = $receiver;
  55. }
  56. /**
  57. * 执行方法
  58. */
  59. public function execute() {
  60. $this->_receiver->copy();
  61. }
  62. }
  63. /**
  64. * 具体拷贝命令角色
  65. */
  66. class PasteCommand implements Command {
  67. private $_receiver;
  68. public function __construct(Receiver $receiver) {
  69. $this->_receiver = $receiver;
  70. }
  71. /**
  72. * 执行方法
  73. */
  74. public function execute() {
  75. $this->_receiver->paste();
  76. }
  77. }
  78. /**
  79. * 接收者角色
  80. */
  81. class Receiver {
  82. /* 接收者名称 */
  83. private $_name;
  84. public function __construct($name) {
  85. $this->_name = $name;
  86. }
  87. /**
  88. * 复制方法
  89. */
  90. public function copy() {
  91. echo $this->_name, ' do copy action.<br />';
  92. }
  93. /**
  94. * 粘贴方法
  95. */
  96. public function paste() {
  97. echo $this->_name, ' do paste action.<br />';
  98. }
  99. }
  100. /**
  101. * 请求者角色
  102. */
  103. class Invoker {
  104. private $_command;
  105. public function __construct(Command $command) {
  106. $this->_command = $command;
  107. }
  108. public function action() {
  109. $this->_command->execute();
  110. }
  111. }
  112. /**
  113. * 客户端
  114. */
  115. class Client {
  116. /**
  117. * Main program.
  118. */
  119. public static function main() {
  120. $receiver = new Receiver('phpppan');
  121. $pasteCommand = new PasteCommand($receiver);
  122. $copyCommand = new CopyCommand($receiver);
  123. $macroCommand = new DemoMacroCommand();
  124. $macroCommand->add($copyCommand);
  125. $macroCommand->add($pasteCommand);
  126. $invoker = new Invoker($macroCommand);
  127. $invoker->action();
  128. $macroCommand->remove($copyCommand);
  129. $invoker = new Invoker($macroCommand);
  130. $invoker->action();
  131. }
  132. }
  133. Client::main();
  134. ?>

以上就是使用php实现命令模式的代码,还有一些关于命令模式的概念区分,希望对大家的学习有所帮助。

人气教程排行