PHP設(shè)計模式之責(zé)任鏈模式的深入解析
更新時間:2013年06月13日 15:43:26 投稿:jingxian
本篇文章是對PHP設(shè)計模式中的責(zé)任鏈模式進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
復(fù)制代碼 代碼如下:
/**
* 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";
關(guān)于PHP責(zé)任鏈設(shè)計模式的一些實(shí)現(xiàn)說明:
◆責(zé)任鏈可能已經(jīng)存在于對象圖中,和復(fù)合模式的例子一樣;
◆此外,Handler抽象可能存在,也可能不存在,最好的選擇是一個分開的Handler接口只可以執(zhí)行handleRequest()操作,不要強(qiáng)制一個鏈只在一個層次中,因為后面的已經(jīng)存在了;
◆也可能引入一個抽象類,但由于請求處理是一個正交關(guān)注,因此具體的類可能已經(jīng)繼承了其它類;
◆通過constructor 或setter,Handler(或下一個Handler)被注入到Client或前一個Handler;
◆請求對象通常是一個ValueObject,也可能被實(shí)現(xiàn)為一個Flyweight,在PHP中,它可能是一個標(biāo)量類型,如string,注意在某些語言中,一個string就是一個不變的ValueObject。
簡單的總結(jié)責(zé)任鏈模式,可以歸納為:用一系列類(classes)試圖處理一個請求request,這些類之間是一個松散的耦合,唯一共同點(diǎn)是在他們之間傳遞request. 也就是說,來了一個請求,A類先處理,如果沒有處理,就傳遞到B類處理,如果沒有處理,就傳遞到C類處理,就這樣象一個鏈條(chain)一樣傳遞下去。
相關(guān)文章
php+ajax登錄跳轉(zhuǎn)登錄實(shí)現(xiàn)思路
這篇文章主要介紹了php+ajax登錄跳轉(zhuǎn)登錄實(shí)現(xiàn)思路,非常的簡單,有需要的小伙伴可以參考下2016-07-07
php計劃任務(wù)之ignore_user_abort函數(shù)實(shí)現(xiàn)方法
這篇文章主要介紹了php計劃任務(wù)之ignore_user_abort函數(shù)實(shí)現(xiàn)方法,以實(shí)例形式分析了php計劃任務(wù)的ignore_user_abort函數(shù)實(shí)現(xiàn)方法,并對ignore_user_abort函數(shù)的用法進(jìn)行了較為詳盡的分析說明,需要的朋友可以參考下2015-01-01

