服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服务器之家 - 编程语言 - PHP教程 - php设计模式 Command(命令模式)

php设计模式 Command(命令模式)

2019-11-29 14:54PHP教程网 PHP教程

将一个请求封装为一个对象从而使你可用不同的请求对客户进行参数化,对请求排除或记录请求日志,以及支持可取消的操作

  1. <?php  
  2. /**  
  3. * 命令模式  
  4.  
  5. * 将一个请求封装为一个对象从而使你可用不同的请求对客户进行参数化,对请求排除或记录请求日志,以及支持可取消的操作  
  6. */  
  7. interface Command  
  8. {  
  9. public function execute();  
  10. }  
  11.  
  12. class Invoker  
  13. {  
  14. private $_command = array();  
  15. public function setCommand($command) {  
  16. $this->_command[] = $command;  
  17. }  
  18.  
  19. public function executeCommand()  
  20. {  
  21. foreach($this->_command as $command)  
  22. {  
  23. $command->execute();  
  24. }  
  25. }  
  26.  
  27. public function removeCommand($command)  
  28. {  
  29. $key = array_search($command, $this->_command);  
  30. if($key !== false)  
  31. {  
  32. unset($this->_command[$key]);  
  33. }  
  34. }  
  35. }  
  36.  
  37. class Receiver  
  38. {  
  39. private $_name = null;  
  40.  
  41. public function __construct($name) {  
  42. $this->_name = $name;  
  43. }  
  44.  
  45. public function action()  
  46. {  
  47. echo $this->_name." action<br/>";  
  48. }  
  49.  
  50. public function action1()  
  51. {  
  52. echo $this->_name." action1<br/>";  
  53. }  
  54. }  
  55.  
  56. class ConcreteCommand implements Command  
  57. {  
  58. private $_receiver;  
  59. public function __construct($receiver)  
  60. {  
  61. $this->_receiver = $receiver;  
  62. }  
  63.  
  64. public function execute()  
  65. {  
  66. $this->_receiver->action();  
  67. }  
  68. }  
  69.  
  70. class ConcreteCommand1 implements Command  
  71. {  
  72. private $_receiver;  
  73. public function __construct($receiver)  
  74. {  
  75. $this->_receiver = $receiver;  
  76. }  
  77.  
  78. public function execute()  
  79. {  
  80. $this->_receiver->action1();  
  81. }  
  82. }  
  83.  
  84. class ConcreteCommand2 implements Command  
  85. {  
  86. private $_receiver;  
  87. public function __construct($receiver)  
  88. {  
  89. $this->_receiver = $receiver;  
  90. }  
  91.  
  92. public function execute()  
  93. {  
  94. $this->_receiver->action();  
  95. $this->_receiver->action1();  
  96. }  
  97. }  
  98.  
  99.  
  100. $objRecevier = new Receiver("No.1");  
  101. $objRecevier1 = new Receiver("No.2");  
  102. $objRecevier2 = new Receiver("No.3");  
  103.  
  104. $objCommand = new ConcreteCommand($objRecevier);  
  105. $objCommand1 = new ConcreteCommand1($objRecevier);  
  106. $objCommand2 = new ConcreteCommand($objRecevier1);  
  107. $objCommand3 = new ConcreteCommand1($objRecevier1);  
  108. $objCommand4 = new ConcreteCommand2($objRecevier2); // 使用 Recevier的两个方法  
  109.  
  110. $objInvoker = new Invoker();  
  111. $objInvoker->setCommand($objCommand);  
  112. $objInvoker->setCommand($objCommand1);  
  113. $objInvoker->executeCommand();  
  114. $objInvoker->removeCommand($objCommand1);  
  115. $objInvoker->executeCommand();  
  116.  
  117. $objInvoker->setCommand($objCommand2);  
  118. $objInvoker->setCommand($objCommand3);  
  119. $objInvoker->setCommand($objCommand4);  
  120. $objInvoker->executeCommand();  

延伸 · 阅读

精彩推荐