PHP设计模式之责任链模式的深入解析

 更新时间:2013年06月13日 15:43:26   投稿:jingxian  
本篇文章是对PHP设计模式中的责任链模式进行了详细的分析介绍,需要的朋友参考下

复制代码 代码如下:

/** 
 * The Handler abstraction. Objects that want to be a part of the 
 * ChainOfResponsibility must implement this interface directly or via 
 * inheritance from an AbstractHandler. 
 */
interface KeyValueStore 

    /** 
     * Obtain a value. 
     * @param string $key 
     * @return mixed 
     */
    public function get($key); 

 
/** 
 * Basic no-op implementation which ConcreteHandlers not interested in 
 * caching or in interfering with the retrieval inherit from. 
 */
abstract class AbstractKeyValueStore implements KeyValueStore 

    protected $_nextHandler; 
 
    public function get($key) 
    { 
 return $this->_nextHandler->get($key); 
    } 

 
/** 
 * Ideally the last ConcreteHandler in the chain. At least, if inserted in 
 * a Chain it will be the last node to be called. 
 */
class SlowStore implements KeyValueStore 

    /** 
     * This could be a somewhat slow store: a database or a flat file. 
     */
    protected $_values; 
 
    public function __construct(array $values = array()) 
    { 
 $this->_values = $values; 
    } 
 
    public function get($key) 
    { 
 return $this->_values[$key]; 
    } 

 
/** 
 * A ConcreteHandler that handles the request for a key by looking for it in 
 * its own cache. Forwards to the next handler in case of cache miss. 
 */
class InMemoryKeyValueStore implements KeyValueStore 

    protected $_nextHandler; 
    protected $_cached = array(); 
 
    public function __construct(KeyValueStore $nextHandler) 
    { 
 $this->_nextHandler = $nextHandler; 
    } 
 
    protected function _load($key) 
    { 
 if (!isset($this->_cached[$key])) { 
     $this->_cached[$key] = $this->_nextHandler->get($key); 
 } 
    } 
 
    public function get($key) 
    { 
 $this->_load($key); 
 return $this->_cached[$key]; 
    } 

 
/** 
 * A ConcreteHandler that delegates the request without trying to 
 * understand it at all. It may be easier to use in the user interface 
 * because it can specialize itself by defining methods that generates 
 * html, or by addressing similar user interface concerns. 
 * Some Clients see this object only as an instance of KeyValueStore 
 * and do not care how it satisfy their requests, while other ones 
 * may use it in its entirety (similar to a class-based adapter). 
 * No client knows that a chain of Handlers exists. 
 */
class FrontEnd extends AbstractKeyValueStore 

    public function __construct(KeyValueStore $nextHandler) 
    { 
 $this->_nextHandler = $nextHandler; 
    } 
 
    public function getEscaped($key) 
    { 
 return htmlentities($this->get($key), ENT_NOQUOTES, 'UTF-8'); 
    } 

 
// Client code 
$store = new SlowStore(array('pd' => 'Philip K. Dick', 
 'ia' => 'Isaac Asimov', 
 'ac' => 'Arthur C. Clarke', 
 'hh' => 'Helmut Heißenbüttel')); 
// in development, we skip cache and pass $store directly to FrontEnd 
$cache = new InMemoryKeyValueStore($store); 
$frontEnd = new FrontEnd($cache); 
 
echo $frontEnd->get('ia'), "\n"; 
echo $frontEnd->getEscaped('hh'), "\n";

关于PHP责任链设计模式的一些实现说明:
◆责任链可能已经存在于对象图中,和复合模式的例子一样;
◆此外,Handler抽象可能存在,也可能不存在,最好的选择是一个分开的Handler接口只可以执行handleRequest()操作,不要强制一个链只在一个层次中,因为后面的已经存在了;
◆也可能引入一个抽象类,但由于请求处理是一个正交关注,因此具体的类可能已经继承了其它类;
◆通过constructor 或setter,Handler(或下一个Handler)被注入到Client或前一个Handler;
◆请求对象通常是一个ValueObject,也可能被实现为一个Flyweight,在PHP中,它可能是一个标量类型,如string,注意在某些语言中,一个string就是一个不变的ValueObject。

简单的总结责任链模式,可以归纳为:用一系列类(classes)试图处理一个请求request,这些类之间是一个松散的耦合,唯一共同点是在他们之间传递request. 也就是说,来了一个请求,A类先处理,如果没有处理,就传递到B类处理,如果没有处理,就传递到C类处理,就这样象一个链条(chain)一样传递下去。

相关文章

  • 纯php生成随机密码

    纯php生成随机密码

    这篇文章主要介绍了纯php生成12位随机密码,生成密码安全可靠,感兴趣的小伙伴们可以参考一下
    2015-10-10
  • php5.4传引用时报错问题分析

    php5.4传引用时报错问题分析

    这篇文章主要介绍了php5.4传引用时报错问题,结合实例形式分析了php5.4传引用时报错问题及解决方法,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-01-01
  • 一个PHP的String类代码

    一个PHP的String类代码

    PHP String 类,暂时只有encode,decode方法
    2010-04-04
  • php+ajax登录跳转登录实现思路

    php+ajax登录跳转登录实现思路

    这篇文章主要介绍了php+ajax登录跳转登录实现思路,非常的简单,有需要的小伙伴可以参考下
    2016-07-07
  • PHP基于自定义类随机生成姓名的方法示例

    PHP基于自定义类随机生成姓名的方法示例

    这篇文章主要介绍了PHP基于自定义类随机生成姓名的方法,结合实例形式分析了php基于数组与字符串的随机数操作生成姓名的相关实现技巧,需要的朋友可以参考下
    2017-08-08
  • PHP设计模式 注册表模式

    PHP设计模式 注册表模式

    注册表模式其实是一个单例模式,注册表类提供静态方法(或单例对象的实例化方法)来让其它对象访问其中的数据(通常是对象)。整个系统中的每个对象都可以访问这些数据对象
    2012-02-02
  • php计划任务之ignore_user_abort函数实现方法

    php计划任务之ignore_user_abort函数实现方法

    这篇文章主要介绍了php计划任务之ignore_user_abort函数实现方法,以实例形式分析了php计划任务的ignore_user_abort函数实现方法,并对ignore_user_abort函数的用法进行了较为详尽的分析说明,需要的朋友可以参考下
    2015-01-01
  • 详解php协程知识点

    详解php协程知识点

    本篇文章给大家分享了关于PHP协程的相关知识点内容,有需要的朋友们可以学习参考下。
    2018-09-09
  • 微信公众平台开发教程②微信端分享功能图文详解

    微信公众平台开发教程②微信端分享功能图文详解

    这篇文章主要介绍了微信公众平台开发微信端分享功能,结合图文形式详细分析了微信分享功能的原理、操作步骤及相关实现技巧,需要的朋友可以参考下
    2019-04-04
  • flash+php+mysql打造简单留言本教程

    flash+php+mysql打造简单留言本教程

    刚开始做这个留言本的时候,连mysql和php都没接触过。经过痛苦的查找资料和学习,郁闷了一个星期后完成了我的简单留言本
    2008-07-07

最新评论