這個設計模式 " /> 成年人网站在线免费观看,欧美一级视,91精品乱码一区二区三区

一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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)一樣傳遞下去。

php技術PHP設計模式之責任鏈模式的深入解析,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 伊人福利在线 | 国产精品亚洲综合一区在线观看 | 18女人腿打开无遮掩免费 | 欧美一级淫片a免费播放口aaa | 小视频免费在线观看 | 亚洲精品国产自在久久老牛 | 色资源在线 | 国产天堂在线观看 | 久久婷婷国产精品香蕉 | 久久综合久久久久 | 欧美性巨大欧美 | 天堂资源在线官网资源 | 久久不卡日韩美女 | 又黄又爽的男女视频 | 亚洲美女视频网址 | 精品国产91久久久久久久 | 91小视频在线观看免费版高清 | 久久成人免费观看全部免费 | 精品久久天干天天天按摩 | 深夜福利小视频 | 国产精品伦理一区二区三区 | 欧美大尺度无遮挡性视频 | 欧美一级一一特黄 | 四虎影视永久免费观看网址 | 91精品全国免费观看 | 婷婷色激情 | 国产成人午夜精品免费视频 | 国产免费久久精品99久久 | 亚洲精品国产精品国自产 | 看免费人成va视频全 | 色多多网站在线观看 | 国产精品免费观看视频播放 | 精品一区二区三区四区在线 | 五月婷花 | 精品久| 4hc44www四虎永久 | 成人看的午夜免费毛片 | 黄色在线观看视频网站 | 日本免费一区二区三区视频 | 国产亚洲精品视频中文字幕 | 国产91免费视频 |