PHP设计模式漫谈之调解者模式

原创
开发 后端 前端
当PHP中对象的关系和依赖发生冲突时,我们可以使用调解者模式在耦合的对象之间协调工作流,有效的防止对象之间相互干扰。

【51CTO独家特稿】本周的《PHP设计模式漫谈》系列,我们将给大家介绍调解者模式,这个模式的目的是封装一组对象之间的相互作用,防止对象之间相互干扰,调解者(Mediator)在同事对象(Colleague)之间充当中间汇聚点。

同事对象之间应该保持松散耦合,避免一个对象直接明确指向另一个对象。在调解者模式下,对象的关系和依赖发生冲突时,我们可以使用调解者在耦合的对象之间协调工作流,依赖可以从同事朝调解者或从调解者向同事建立,这两个方向上的依赖都可以使用AbstractColleague或AbstractMediator中断。

调解者和同事对象 
图1 调解者和同事对象

对象不是孤立的,对象之间必须相互协作才能完成任务。虽然调解者模式可以限制对象之间的相互作用,但如果滥用,会致使编写聚合性类变得非常困难。举一个实用的例子,在领域驱动设计(Domain-Driven Design)中的服务就是实体之间的调解者。再举一个PHP相关的例子,Zend_Form装饰和过滤功能实际上可以看作是Zend_Form_Decorator和Zend_Filter实例之间的一个简单调解者,它们都使用Zend_Validate对象进行验证。

当调解者必须监听同事对象的事件时,它通常是作为观察者(Observer)实现的,产生一个黑板(blackboard)对象,一些同事写,另一些同事就读。来自同事的事件被推向调解者,再由调解者将其转发给其它订阅的同事,同事之间不需要相互了解,这个架构成功用于随Zend框架发布的Dojo JavaScript库。这个模式的另一个好处是对象的变化包含在计算方法中,可以通过配置不同的调解者实现这一目标,但实例化相关对象将是一个松散的操作,不同容器和工厂之间的协作关系将是分散的。参与者:

◆同事(Colleague):重点是它的职责,它只与一个调解者Mediator或AbstractMediator通信。

◆调解者(Mediator):协同多个Colleagues(AbstractColleagues)共同工作。

◆AbstractMediator,AbstractColleague:从这些角色的真实实现解耦的可选接口,可能不止一个AbstractColleague角色。

下面的代码实现了一个表单输入的过滤过程,类似于Zend_Form_Element功能。

  1. <?php 
  2. /**  
  3.  * AbstractColleague.  
  4.  */  
  5. interface Filter  
  6. {  
  7.     public function filter($value);  
  8. }  
  9.  
  10. /**  
  11.  * Colleague. We decide in the implementation phase  
  12.  * that Colleagues should not know the next Colleague  
  13.  * in the chain, resorting to a Mediator to link them together.  
  14.  * This choice succesfully avoids a base abstract class  
  15.  * for Filters.  
  16.  * Remember that this is an example: it is not only  
  17.  * Chain of Responsibility that can be alternatively implemented  
  18.  * as a Mediator.  
  19.  */  
  20. class TrimFilter implements Filter  
  21. {  
  22.      public function filter($value)  
  23.      {  
  24.          return trim($value);  
  25.      }  
  1. /**  
  2.  * Colleague.  
  3.  */  
  4. class NullFilter implements Filter  
  5. {  
  6.      public function filter($value)  
  7.      {  
  8.          return $value ? $value : '';  
  9.      }  
  10. }  
  11.  
  12. /**  
  13.  * Colleague.  
  14.  */  
  15. class HtmlEntitiesFilter implements Filter  
  16. {  
  17.      public function filter($value)  
  18.      {  
  19.          return htmlentities($value);  
  20.      }  
  1. /**  
  2.  * The Mediator. We avoid referencing it from ConcreteColleagues  
  3.  * and so the need for an interface. We leave the implementation  
  4.  * of a bidirectional channel for the Observer pattern's example.  
  5.  * This class responsibility is to store the value and coordinate  
  6.  * filters computation when they have to be applied to the value.  
  7.  * Filtering responsibilities are obviously a concern of  
  8.  * the Colleagues, which are Filter implementations.  
  9.  */  
  10. class InputElement  
  11. {  
  12.     protected $_filters;  
  13.     protected $_value;  
  14.  
  15.     public function addFilter(Filter $filter)  
  16.     {  
  17.         $this->_filters[] = $filter;  
  18.         return $this;  
  19.     }  
  20.  
  21.     public function setValue($value)  
  22.     {  
  23.         $this->_value = $this->_filter($value);  
  24.     }  
  25.  
  26.     protected function _filter($value)  
  27.     {  
  28.         foreach ($this->_filters as $filter) {  
  29.             $value = $filter->filter($value);  
  30.         }  
  31.         return $value;  
  32.     }  
  33.  
  34.     public function getValue()  
  35.     {  
  36.         return $this->_value;  
  37.     }  
  38. }  
  39.  
  40. $input = new InputElement();  
  41. $input->addFilter(new NullFilter())  
  42.       ->addFilter(new TrimFilter())  
  43.       ->addFilter(new HtmlEntitiesFilter());  
  44. $input->setValue(' You should use the <h1>-<h6> tags for your headings.');  
  45. echo $input->getValue(), "\n"; 

原文名:Practical Php Patterns: Mediator      作者:Giorgio

原文出处:http://giorgiosironi.blogspot.com/search/label/practical%20php%20patterns

【PHP设计模式漫谈】

  1. PHP设计模式漫谈之迭代器模式
  2. PHP设计模式漫谈之解释器模式
  3. PHP设计模式漫谈之工厂模式
  4. PHP设计模式漫谈之命令模式
  5. PHP设计模式漫谈之结构模式
责任编辑:王晓东 来源: 51CTO.com
相关推荐

2010-04-13 08:54:28

PHP设计模式命令模式

2010-04-19 09:30:00

工厂模式PHP设计模式

2010-03-25 08:52:30

PHP设计模式代理模式

2010-04-29 08:53:11

PHP迭代器模式

2010-04-01 09:10:03

PHP设计模式责任链模式

2010-04-08 09:27:04

PHP设计模式结构模式

2010-04-21 08:38:18

解释器模式PHP设计模式

2020-12-01 07:16:05

重学设计模式

2021-01-21 05:34:14

设计模式建造者

2021-07-08 11:28:43

观察者模式设计

2012-05-16 17:15:04

Java设计模式

2011-04-21 09:46:41

设计模式

2021-10-26 00:21:19

设计模式建造者

2020-10-20 13:33:00

建造者模式

2020-11-05 09:38:07

中介者模式

2020-08-21 07:23:50

工厂模式设计

2020-10-26 08:45:39

观察者模式

2023-05-26 08:41:23

模式Go设计模式

2015-09-08 13:39:10

JavaScript设计模式

2012-01-13 15:59:07

点赞
收藏

51CTO技术栈公众号