六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 843|回复: 0

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

[复制链接]
 楼主| 发表于 2013-8-1 15:00:23 | 显示全部楼层 |阅读模式
  1. <?php
  2. /**
  3. * 命令模式
  4. *
  5. * 将一个请求封装为一个对象从而使你可用不同的请求对客户进行参数化,对请求排除或记录请求日志,以及支持可取消的操作
  6. */
  7. interface Command
  8. {
  9. publicfunction execute();
  10. }

  11. class Invoker
  12. {
  13. private$_command=array();
  14. publicfunction setCommand($command) {
  15. $this->_command[] =$command;
  16.     }

  17. publicfunction executeCommand()
  18.     {
  19. foreach($this->_command as$command)
  20.         {
  21. $command->execute();
  22.         }
  23.     }

  24. publicfunction removeCommand($command)
  25.     {
  26. $key=array_search($command,$this->_command);
  27. if($key!==false)
  28.         {
  29. unset($this->_command[$key]);
  30.         }
  31.     }
  32. }

  33. class Receiver
  34. {
  35. private$_name=null;

  36. publicfunction __construct($name) {
  37. $this->_name =$name;
  38.     }

  39. publicfunction action()
  40.     {
  41. echo$this->_name." action
  42. ";
  43.     }

  44. publicfunction action1()
  45.     {
  46. echo$this->_name." action1
  47. ";
  48.     }
  49. }

  50. class ConcreteCommand implements Command
  51. {
  52. private$_receiver;
  53. publicfunction __construct($receiver)
  54.     {
  55. $this->_receiver =$receiver;
  56.     }

  57. publicfunction execute()
  58.     {
  59. $this->_receiver->action();
  60.     }
  61. }

  62. class ConcreteCommand1 implements Command
  63. {
  64. private$_receiver;
  65. publicfunction __construct($receiver)
  66.     {
  67. $this->_receiver =$receiver;
  68.     }

  69. publicfunction execute()
  70.     {
  71. $this->_receiver->action1();
  72.     }
  73. }

  74. class ConcreteCommand2 implements Command
  75. {
  76. private$_receiver;
  77. publicfunction __construct($receiver)
  78.     {
  79. $this->_receiver =$receiver;
  80.     }

  81. publicfunction execute()
  82.     {
  83. $this->_receiver->action();
  84. $this->_receiver->action1();
  85.     }
  86. }


  87. $objRecevier=new Receiver("No.1");
  88. $objRecevier1=new Receiver("No.2");
  89. $objRecevier2=new Receiver("No.3");

  90. $objCommand=new ConcreteCommand($objRecevier);
  91. $objCommand1=new ConcreteCommand1($objRecevier);
  92. $objCommand2=new ConcreteCommand($objRecevier1);
  93. $objCommand3=new ConcreteCommand1($objRecevier1);
  94. $objCommand4=new ConcreteCommand2($objRecevier2); // 使用 Recevier的两个方法

  95. $objInvoker=new Invoker();
  96. $objInvoker->setCommand($objCommand);
  97. $objInvoker->setCommand($objCommand1);
  98. $objInvoker->executeCommand();
  99. $objInvoker->removeCommand($objCommand1);
  100. $objInvoker->executeCommand();

  101. $objInvoker->setCommand($objCommand2);
  102. $objInvoker->setCommand($objCommand3);
  103. $objInvoker->setCommand($objCommand4);
  104. $objInvoker->executeCommand();
复制代码
本文摘自:http://www.cnblogs.com/bluefrog/archive/2011/06/16/2082252.html
代码包下载:
(传统的23种模式(没有区分简单工厂与抽象工厂)
http://it.agoit.com/thread-419151-1-1.html  php设计模式 Interpreter(解释器模式)
http://it.agoit.com/thread-419152-1-1.html  php设计模式 Factory(工厂模式)
http://it.agoit.com/thread-419153-1-1.html  php设计模式 Facade(外观模式)
http://it.agoit.com/thread-419154-1-1.html  php设计模式 Decorator(装饰模式)
http://it.agoit.com/thread-419155-1-1.html  php设计模式 Builder(建造者模式)
http://it.agoit.com/thread-419156-1-1.html  php设计模式 Adapter(适配器模式)
http://it.agoit.com/thread-419157-1-1.html  php设计模式 Template (模板模式)
http://it.agoit.com/thread-419158-1-1.html  php设计模式 Command(命令模式)
http://it.agoit.com/thread-419159-1-1.html  php设计模式 Singleton(单例模式)
http://it.agoit.com/thread-419160-1-1.html  php设计模式 Observer(观察者模式)
http://it.agoit.com/thread-419161-1-1.html  php设计模式 Strategy(策略模式)
http://it.agoit.com/thread-419162-1-1.html  php设计模式 Visitor (访问者模式)
http://it.agoit.com/thread-419163-1-1.html  php设计模式 Memento (备忘录模式)
http://it.agoit.com/thread-419164-1-1.html php设计模式 Prototype (原型模式)
http://it.agoit.com/thread-419165-1-1.html php设计模式 Mediator (中介者模式)
http://it.agoit.com/thread-419166-1-1.html php设计模式 FlyWeight (享元模式)
http://it.agoit.com/thread-419167-1-1.html php设计模式 Chain Of Responsibility (职责链模式)
http://it.agoit.com/thread-419168-1-1.html php设计模式 Bridge (桥接模式)
http://it.agoit.com/thread-419169-1-1.html php设计模式 Proxy (代理模式)
http://it.agoit.com/thread-419170-1-1.html php设计模式 State (状态模式)
http://it.agoit.com/thread-419171-1-1.html php设计模式 Composite (组合模式)
http://it.agoit.com/thread-419172-1-1.html php设计模式 Interator (迭代器模式)
下面来自<<php设计模式>>
http://it.agoit.com/thread-419173-1-1.html php设计模式 DAO(数据访问对象模式)
http://it.agoit.com/thread-419174-1-1.html php设计模式 Delegation(委托模式)



该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表