六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 765|回复: 0

php设计模式 Mediator (中介者模式)

[复制链接]
 楼主| 发表于 2013-8-1 15:36:25 | 显示全部楼层 |阅读模式
  1. <?php
  2. /**
  3. * 中介者模式
  4. *
  5. * 用一个中介对象来封装一系列的对象交互,使各对象不需要显式地相互引用从而使其耦合松散,而且可以独立地改变它们之间的交互
  6. */
  7. abstractclass Mediator
  8. {
  9. abstractpublicfunction send($message,$colleague);
  10. }

  11. abstractclass Colleague
  12. {
  13. private$_mediator=null;

  14. publicfunction Colleague($mediator)
  15.     {
  16. $this->_mediator =$mediator;
  17.     }

  18. publicfunction send($message)
  19.     {
  20. $this->_mediator->send($message,$this);
  21.     }

  22. abstractpublicfunction notify($message);
  23. }

  24. class ConcreteMediator extends Mediator
  25. {
  26. private$_colleague1=null;
  27. private$_colleague2=null;

  28. publicfunction send($message,$colleague)
  29.     {
  30. if($colleague==$this->_colleague1)
  31.         {
  32. $this->_colleague1->notify($message);
  33.         } else {
  34. $this->_colleague2->notify($message);
  35.         }
  36.     }

  37. publicfunction set($colleague1,$colleague2)
  38.     {
  39. $this->_colleague1 =$colleague1;
  40. $this->_colleague2 =$colleague2;
  41.     }
  42. }

  43. class Colleague1 extends Colleague
  44. {
  45. publicfunction notify($message)
  46.     {
  47. echo"Colleague1 Message is :".$message."
  48. ";
  49.     }
  50. }

  51. class Colleague2 extends Colleague
  52. {
  53. publicfunction notify($message)
  54.     {
  55. echo"Colleague2 Message is :".$message."
  56. ";
  57.     }
  58. }

  59. //
  60. $objMediator=new ConcreteMediator();
  61. $objC1=new Colleague1($objMediator);
  62. $objC2=new Colleague2($objMediator);

  63. $objMediator->set($objC1,$objC2);

  64. $objC1->send("to c2 from c1");
  65. $objC2->send("to c1 from c2");
复制代码
本文摘自:http://www.cnblogs.com/bluefrog/archive/2011/06/24/2089406.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(委托模式)



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博账号登陆

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

本版积分规则

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