PHP设计模式之责任链模式的深入解析
程序员文章站
2022-10-06 10:50:49
责任链模式,其目的是组织一个对象链处理一个如方法调用的请求。当concretehandler(具体的处理程序)不知道如何满足来自client的请求时,或它的目的不是这个时,...
责任链模式,其目的是组织一个对象链处理一个如方法调用的请求。
当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)一样传递下去。
当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)一样传递下去。
下一篇: 通过ICQ网关发送手机短信的PHP源程序