php中调解者设计模式

参考:http://developer.51cto.com/art/201005/198222.htm ,

下面的代码全是自个调试通过的

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

代码:

<?php
/**
* 调解者设计模式
* 的目的是封装一组对象之间的相互作用,
* 防止对象之间相互干扰,
* 调解者(Mediator)在同事对象(Colleague)之间充当中间汇聚点。
* @author lichaoyong 2014-04-21 13:20:17
* 
* 思路:同事对象之间应该保持松散耦合,避免一个对象直接明确指向另一个对象
* 
* 调解者模式下,对象的关系和依赖发生冲突时,
* 我们可以使用调解者在耦合的对象之间协调工作流,
* 依赖可以从同事朝调解者或从调解者向同事建立,
* 这两个方向上的依赖都可以使用AbstractColleague或AbstractMediator中断
*/
require 'filter.php';
require 'html_entities_filter.php';
require 'input_element.php';
require 'null_filter.php';
require 'trim_filter.php';
require 'strip_tags1.php';

class main{
  //程序主入口
  public function main(){
      $input = new input_element();
      $input->add_filter(new null_filter());
      $input->add_filter(new trim_filter());
      //先去除html标签,转义之后就不是html标签了
      $input->add_filter(new strip_tags1());
      //最后才html转义
      $input->add_filter(new html_entities_filter());
      $input->set_value('this is set value地方斯蒂芬<ff>');
      
      echo $input->get_value();
  }
}
?>
<?php
/**
* this is a colleague
*@author lichaoyong 2014-04-21 13:30:54
*/
  class null_filter implements filter{
      
      /**
      * 实现接口并且实现接口中的所有method
      */
      public function filter($value){
          return trim($value);
      }
  }
?>

<?php
/**
* 去除html标签
*/
  class strip_tags1 implements filter{
     /**
      * 实现接口并且实现接口中的所有method
      */
      public function filter($value){
          //去掉html标签,要先转换成html标签的
          //这三个的区别
//          htmlspecialchars();  
//          htmlentities();
//          htmlspecialchars_decode();
          return strip_tags($value);
      }
      
  }
?>

<?php
  class trim_filter implements filter{
      /**
      * 实现接口,并且实现接口中所有method
      * 
      */
      public function filter($value){
        return trim($value);
      }
  }
?>

<?php
/**
* 用于对输入的各种节点进行,过滤,【例如除去html表情,特殊字符】
* @author lichaoyong 2014-04-21 17:25:37
*/
  class input_element{
      protected $_filters;
      protected $_value;
      
      /**
      * 增加各种版本的过滤器
      * @author lichaoyong 2014-04-21 17:17:27
      */
      public function add_filter($filter){
          if(!interface_exists('filter')){
              echo 'not found class filter';
              exit;
          }
          $this->_filters[] = $filter;
          
          return $this;
      }
      
      /**
      * 给节点设置值
      * @author lichaoyong 2014-04-21 17:17:51
      */
      public function set_value($value){
          $this->_value = $this->_filter($value);
      }
      
      /**
      * 传入需要过滤的值,使用对象数组【所有的过滤器】,去过滤值
      * @author lichaoyong 2014-04-21 17:18:06
      * 
      */
      protected function _filter(&$value){          
          foreach($this->_filters as $filter_object){
              $value = $filter_object->filter($value);
          }
          //使用过滤器,过完该值的所有可能性,最终返回正确结果
          return $value;
      }
      
      public function get_value(){
          return $this->_value;
      }
  }
?>

<?php
/**
* this is a colleague
* @author lichaoyong 2014-04-21 13:32:19
*/
  class html_entities_filter implements filter{
      
      public function filter($value){
          return htmlentities($value);
      }
  }
?>

<?php
  /**
  * 抽象的同事
  * abstract_colleague
  * @author lichaoyong 2014-04-21 13:25:09
  */
  interface filter{
    public function filter($value);
  }
?>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值