PHP设计模式之调解者模式的深入解析

yipeiwu_com6年前PHP代码库

调解者模式,这个模式的目的是封装一组对象之间的相互作用,防止对象之间相互干扰,调解者(Mediator)在同事对象(Colleague)之间充当中间汇聚点。同事对象之间应该保持松散耦合,避免一个对象直接明确指向另一个对象。在调解者模式下,对象的关系和依赖发生冲突时,我们可以使用调解者在耦合的对象之间协调工作流,依赖可以从同事朝调解者或从调解者向同事建立,这两个方向上的依赖都可以使用AbstractColleague或AbstractMediator中断。



对象不是孤立的,对象之间必须相互协作才能完成任务。虽然调解者模式可以限制对象之间的相互作用,但如果滥用,会致使编写聚合性类变得非常困难。举一个实用的例子,在领域驱动设计(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功能。

复制代码 代码如下:

    <?php
    /** 
     * AbstractColleague. 
     */ 
    interface Filter 
    { 
 public function filter($value); 
    } 

    /** 
     * Colleague. We decide in the implementation phase 
     * that Colleagues should not know the next Colleague 
     * in the chain, resorting to a Mediator to link them together. 
     * This choice succesfully avoids a base abstract class 
     * for Filters. 
     * Remember that this is an example: it is not only 
     * Chain of Responsibility that can be alternatively implemented 
     * as a Mediator. 
     */ 
    class TrimFilter implements Filter 
    { 
  public function filter($value) 
  { 
      return trim($value); 
  } 
    } <PRE class=php name="code">    /** 
     * Colleague. 
     */ 
    class NullFilter implements Filter 
    { 
 public function filter($value) 
 { 
     return $value ? $value : ''; 
 } 
    } 

    /** 
     * Colleague. 
     */ 
    class HtmlEntitiesFilter implements Filter 
    { 
 public function filter($value) 
 { 
     return htmlentities($value); 
 } 
    }
</PRE><PRE class=php name="code">    /** 
     * The Mediator. We avoid referencing it from ConcreteColleagues 
     * and so the need for an interface. We leave the implementation 
     * of a bidirectional channel for the Observer pattern's example. 
     * This class responsibility is to store the value and coordinate 
     * filters computation when they have to be applied to the value. 
     * Filtering responsibilities are obviously a concern of 
     * the Colleagues, which are Filter implementations. 
     */ 
    class InputElement 
    { 
 protected $_filters; 
 protected $_value; 

 public function addFilter(Filter $filter) 
 { 
     $this->_filters[] = $filter; 
     return $this; 
 } 

 public function setValue($value) 
 { 
     $this->_value = $this->_filter($value); 
 } 

 protected function _filter($value) 
 { 
     foreach ($this->_filters as $filter) { 
  $value = $filter->filter($value); 
     } 
     return $value; 
 } 

 public function getValue() 
 { 
     return $this->_value; 
 }   
    } 

    $input = new InputElement(); 
    $input->addFilter(new NullFilter()) 
   ->addFilter(new TrimFilter()) 
   ->addFilter(new HtmlEntitiesFilter()); 
    $input->setValue(' You should use the <h1>-<h6> tags for your headings.'); 
    echo $input->getValue(), "\n";
</PRE>
<PRE></PRE>

相关文章

PHP删除HTMl标签的三种解决方法

方法1:直接取出想要取出的标记复制代码 代码如下:<?php    //取出br标记    function strip...

PHP实现限制IP访问的方法

本文实例讲述了PHP实现限制IP访问的方法。分享给大家供大家参考,具体如下: //获取客户端ip if (getenv("HTTP_CLIENT_IP")) $ip = gete...

PHP目录函数实现创建、读取目录教程实例

今天主要介绍在PHP网站开发中文件目录函数的应用。在PHP网站开发中,我们时常需要读取目录文件信息或者创建目录以存放必要的文件,而当目录文件大小超出规定大小时我们又需要删除目录文件,如手...

PHP sprintf()函数用例解析

复制代码 代码如下: <?php //sprintf()函数,返回值为格式化后的字符串 string sprintf ( string $format [, mixed $args...

php微信公众平台配置接口开发程序

php微信公众平台配置接口开发程序

在阅读这篇文章之前你要做以下的准备: 1.注册一个订阅号(通过微信公众平台https://mp.weixin.qq.com/); 2.注册新浪云并实名认证http://sae.sina....