PHP設(shè)計模式之責(zé)任鏈模式的深入解析
更新時間:2013年06月13日 15:43:26 作者:
本篇文章是對PHP設(shè)計模式中的責(zé)任鏈模式進行了詳細的分析介紹,需要的朋友參考下
責(zé)任鏈模式,其目的是組織一個對象鏈處理一個如方法調(diào)用的請求。
當(dāng)ConcreteHandler(具體的處理程序)不知道如何滿足來自Client的請求時,或它的目的不是這個時,它會委派給鏈中的下一個Handler(處理程序)來處理。
這個設(shè)計模式通常和復(fù)合模式一起使用,其中有些葉子或容器對象默認(rèn)委派操作給它們的父對象。另一個例子是,本地化通常是使用責(zé)任鏈處理的,當(dāng)?shù)抡Z翻譯適配器沒有為翻譯關(guān)鍵詞找到合適的結(jié)果時,就返回到英語適配器或干脆直接顯示關(guān)鍵詞本身。
耦合減少到最低限度:Client類不知道由哪個具體的類來處理請求;在創(chuàng)建對象圖時配置了鏈;ConcreteHandlers不知道哪個對象是它們的繼承者。行為在對象之間分配是成功的,鏈中最近的對象有優(yōu)先權(quán)和責(zé)任滿足請求。
參與者:
◆Client(客戶端):向Handler(處理程序)提交一個請求;
◆Handler(處理程序)抽象:接收一個請求,以某種方式滿足它;
◆ConcreteHandlers(具體的處理程序):接收一個請求,設(shè)法滿足它,如果不成功就委派給下一個處理程序。
下面的代碼實現(xiàn)了一個最著名的責(zé)任鏈?zhǔn)纠憾嗉壘彺妗?BR>
/**
* 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è)計模式的一些實現(xiàn)說明:
◆責(zé)任鏈可能已經(jīng)存在于對象圖中,和復(fù)合模式的例子一樣;
◆此外,Handler抽象可能存在,也可能不存在,最好的選擇是一個分開的Handler接口只可以執(zhí)行handleRequest()操作,不要強制一個鏈只在一個層次中,因為后面的已經(jīng)存在了;
◆也可能引入一個抽象類,但由于請求處理是一個正交關(guān)注,因此具體的類可能已經(jīng)繼承了其它類;
◆通過constructor 或setter,Handler(或下一個Handler)被注入到Client或前一個Handler;
◆請求對象通常是一個ValueObject,也可能被實現(xiàn)為一個Flyweight,在PHP中,它可能是一個標(biāo)量類型,如string,注意在某些語言中,一個string就是一個不變的ValueObject。
簡單的總結(jié)責(zé)任鏈模式,可以歸納為:用一系列類(classes)試圖處理一個請求request,這些類之間是一個松散的耦合,唯一共同點是在他們之間傳遞request. 也就是說,來了一個請求,A類先處理,如果沒有處理,就傳遞到B類處理,如果沒有處理,就傳遞到C類處理,就這樣象一個鏈條(chain)一樣傳遞下去。
當(dāng)ConcreteHandler(具體的處理程序)不知道如何滿足來自Client的請求時,或它的目的不是這個時,它會委派給鏈中的下一個Handler(處理程序)來處理。
這個設(shè)計模式通常和復(fù)合模式一起使用,其中有些葉子或容器對象默認(rèn)委派操作給它們的父對象。另一個例子是,本地化通常是使用責(zé)任鏈處理的,當(dāng)?shù)抡Z翻譯適配器沒有為翻譯關(guān)鍵詞找到合適的結(jié)果時,就返回到英語適配器或干脆直接顯示關(guān)鍵詞本身。
耦合減少到最低限度:Client類不知道由哪個具體的類來處理請求;在創(chuàng)建對象圖時配置了鏈;ConcreteHandlers不知道哪個對象是它們的繼承者。行為在對象之間分配是成功的,鏈中最近的對象有優(yōu)先權(quán)和責(zé)任滿足請求。
參與者:
◆Client(客戶端):向Handler(處理程序)提交一個請求;
◆Handler(處理程序)抽象:接收一個請求,以某種方式滿足它;
◆ConcreteHandlers(具體的處理程序):接收一個請求,設(shè)法滿足它,如果不成功就委派給下一個處理程序。
下面的代碼實現(xiàn)了一個最著名的責(zé)任鏈?zhǔn)纠憾嗉壘彺妗?BR>
復(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è)計模式的一些實現(xiàn)說明:
◆責(zé)任鏈可能已經(jīng)存在于對象圖中,和復(fù)合模式的例子一樣;
◆此外,Handler抽象可能存在,也可能不存在,最好的選擇是一個分開的Handler接口只可以執(zhí)行handleRequest()操作,不要強制一個鏈只在一個層次中,因為后面的已經(jīng)存在了;
◆也可能引入一個抽象類,但由于請求處理是一個正交關(guān)注,因此具體的類可能已經(jīng)繼承了其它類;
◆通過constructor 或setter,Handler(或下一個Handler)被注入到Client或前一個Handler;
◆請求對象通常是一個ValueObject,也可能被實現(xiàn)為一個Flyweight,在PHP中,它可能是一個標(biāo)量類型,如string,注意在某些語言中,一個string就是一個不變的ValueObject。
簡單的總結(jié)責(zé)任鏈模式,可以歸納為:用一系列類(classes)試圖處理一個請求request,這些類之間是一個松散的耦合,唯一共同點是在他們之間傳遞request. 也就是說,來了一個請求,A類先處理,如果沒有處理,就傳遞到B類處理,如果沒有處理,就傳遞到C類處理,就這樣象一個鏈條(chain)一樣傳遞下去。
您可能感興趣的文章:
相關(guān)文章
使用php轉(zhuǎn)義輸出HTML到JavaScript
本文給大家分享的是個人項目中的一個小需求,需要使用php轉(zhuǎn)義輸出HTML到JavaScript,就寫了個function,推薦給大家,希望大家能夠喜歡。2015-03-03PHP 優(yōu)化配置——加速你的VBB,phpwind,Discuz,IPB,MolyX
PHP 優(yōu)化配置——加速你的VBB,phpwind,Discuz,IPB,MolyX...2007-07-07PHP實現(xiàn)的下載遠程圖片自定義函數(shù)分享
這篇文章主要介紹了PHP實現(xiàn)的下載遠程圖片自定義函數(shù)分享,本文直接給出實現(xiàn)代碼和,本文直接給出實現(xiàn)代碼和使用方法,需要的朋友可以參考下2015-01-01將FCKeditor導(dǎo)入PHP+SMARTY的實現(xiàn)方法
這篇文章主要介紹了將FCKeditor導(dǎo)入PHP+SMARTY的實現(xiàn)方法,涉及整合FCKeditor與SMARTY的技巧,非常具有實用價值,需要的朋友可以參考下2015-01-01PHP中功能強大卻很少使用的函數(shù)實例小結(jié)
這篇文章主要介紹了PHP中功能強大卻很少使用的函數(shù),結(jié)合實例形式總結(jié)分析了php中非常實用的幾個函數(shù),包括函數(shù)的調(diào)用、注冊、調(diào)用、判斷等操作技巧,需要的朋友可以參考下2016-11-11PHP正則匹配日期和時間(時間戳轉(zhuǎn)換)的實例代碼
本文介紹下,用php實現(xiàn)正則匹配日期與時間,并進行時間戳轉(zhuǎn)換的例子,有需要的朋友,參考下吧2016-12-12PHP global全局變量經(jīng)典應(yīng)用與注意事項分析【附$GLOBALS用法對比】
這篇文章主要介紹了PHP global全局變量經(jīng)典應(yīng)用與注意事項,結(jié)合實例形式分析了php中g(shù)lobal全局變量的功能、使用方法及相關(guān)操作注意事項,并附帶了$GLOBALS用法作為對比,需要的朋友可以參考下2019-07-07