php中调解者设计模式
来源:互联网 发布:天猫淘宝优惠券发布群 编辑:程序博客网 时间:2024/04/30 03:07
参考: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); }?>
0 0
- php中调解者设计模式
- PHP设计模式漫谈之调解者模式
- PHP设计模式之调解者模式
- PHP设计模式漫谈之调解者模式
- PHP設計模式之调解者模式
- 设计模式22:Mediator Pattern (调解者模式)
- php中备份设计模式
- php中常见设计模式
- PHP设计模式中之装饰模式
- PHP中设计模式的学习笔记
- PHP中常用的设计模式
- 5中常用的php设计模式
- PHP中常见的设计模式
- [php]php设计模式 (总结)
- php设计模式
- php 观察者设计模式
- php设计模式
- php设计模式
- 桌面图标全是问号
- 精致的水晶风格按钮
- 信息论与编码之算术编码
- hdoj2010
- 人人都是产品经理
- php中调解者设计模式
- 学程序开发
- zookeeper应用场景
- 2PL协议,两段封锁协议
- android UEventObserver
- Coder-Strike 2014 - Finals (online edition, Div. 2) D.Bug in Code
- 大小写不敏感的字符串
- zookeeper集群安装配置
- 安装sqlserver 2012后链接数据库时用server=localhost可以访问而用server=127.0.0.1访问不了