≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > PHP笔记

PHP设计模式之责任链模式的简单示例

人气:344 时间:2020-12-12

这篇文章主要为大家详细介绍了PHP设计模式之责任链模式的简单示例,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小玲来看看吧!

责任链模式,其目的是组织一个对象链处理一个如方法调用的请求。
当ConcreteHandler(具体的处理程序)不知道如何满足来自Client的请求时,或它的目的不是这个时,它会委派给链中的下一个Handler(处理程序)来处理。

这个设计模式通常和复合模式一起使用,其中有些叶子或容器对象默认委派操作给它们的父对象。另一个例子是,本地化通常是使用责任链处理的,当德语翻译适配器没有为翻译关键词找到合适的结果时,就返回到英语适配器或干脆直接显示关键词本身。

耦合减少到最低限度:Client类不知道由哪个具体的类来处理请求;在创建对象图时配置了链;ConcreteHandlers不知道哪个对象是它们的继承者。行为在对象之间分配是成功的,链中最近的对象有优先权和责任满足请求。

参与者:
◆Client(客户端):向Handler(处理程序)提交一个请求;
◆Handler(处理程序)抽象:接收一个请求,以某种方式满足它;
◆ConcreteHandlers(具体的处理程序):接收一个请求,设法满足它,如果不成功就委派给下一个处理程序。
下面的代码实现了一个最著名的责任链示例:多级缓存。

代码如下:


/**  
 * 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)一样传递下去。

本文来自:http://www.q1010.com/173/15421-0.html

注:关于PHP设计模式之责任链模式的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:

您可能感兴趣的文章

上一篇:PHP设计模式之调解者模式的简单示例
下一篇:PHP处理换行符 \r\n的深入分析
热门文章
  • PHP 写入WRITE编码为UTF8的文件示例
  • PHP 中文字符串截取函数示例:支持gb2312,gbk,big
  • PHP 简单留言板的制作示例
  • 解决Fatal error: Call to undefined function mb_convert_encoding() in错误问题
  • PHP语言基础(标记、注释、变量、数组、常量、函数)示例
  • php 生成迅雷链接的简单示例
  • php 获取短网址的实现方法
  • PHP 通用分页类的简单示例
  • PHP 使用文件方式导入导出整个MYSQL数据库的实现方法
  • php 获取MYSQL错误的简单示例
  • 最新文章
    • 解决PHP使用redis实现统计缓存MySQL压力的问题
    • php 简单的上传进度条的简单示例
    • php 给html中引用的js和css路径打上版本号的实现方法
    • php 实现计算年龄精准到年月日的实例
    • php+ajax无刷新分页的简单示例
    • 解决php+ajax无刷新上传图片的问题
    • 解决PHP生成HTML静态页面的问题
    • 解决PHP使用uniqid函数生成唯一ID的问题
    • 解决PHP防刷票的一些问题
    • 微信access_token的获取开发的实现方法

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。