最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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生成隨機(jī)密碼

    純php生成隨機(jī)密碼

    這篇文章主要介紹了純php生成12位隨機(jī)密碼,生成密碼安全可靠,感興趣的小伙伴們可以參考一下
    2015-10-10
  • php5.4傳引用時報錯問題分析

    php5.4傳引用時報錯問題分析

    這篇文章主要介紹了php5.4傳引用時報錯問題,結(jié)合實(shí)例形式分析了php5.4傳引用時報錯問題及解決方法,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-01-01
  • 一個PHP的String類代碼

    一個PHP的String類代碼

    PHP String 類,暫時只有encode,decode方法
    2010-04-04
  • php+ajax登錄跳轉(zhuǎn)登錄實(shí)現(xiàn)思路

    php+ajax登錄跳轉(zhuǎn)登錄實(shí)現(xiàn)思路

    這篇文章主要介紹了php+ajax登錄跳轉(zhuǎn)登錄實(shí)現(xiàn)思路,非常的簡單,有需要的小伙伴可以參考下
    2016-07-07
  • PHP基于自定義類隨機(jī)生成姓名的方法示例

    PHP基于自定義類隨機(jī)生成姓名的方法示例

    這篇文章主要介紹了PHP基于自定義類隨機(jī)生成姓名的方法,結(jié)合實(shí)例形式分析了php基于數(shù)組與字符串的隨機(jī)數(shù)操作生成姓名的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-08-08
  • PHP設(shè)計模式 注冊表模式

    PHP設(shè)計模式 注冊表模式

    注冊表模式其實(shí)是一個單例模式,注冊表類提供靜態(tài)方法(或單例對象的實(shí)例化方法)來讓其它對象訪問其中的數(shù)據(jù)(通常是對象)。整個系統(tǒng)中的每個對象都可以訪問這些數(shù)據(jù)對象
    2012-02-02
  • php計劃任務(wù)之ignore_user_abort函數(shù)實(shí)現(xiàn)方法

    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
  • 詳解php協(xié)程知識點(diǎn)

    詳解php協(xié)程知識點(diǎn)

    本篇文章給大家分享了關(guān)于PHP協(xié)程的相關(guān)知識點(diǎn)內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。
    2018-09-09
  • 微信公眾平臺開發(fā)教程②微信端分享功能圖文詳解

    微信公眾平臺開發(fā)教程②微信端分享功能圖文詳解

    這篇文章主要介紹了微信公眾平臺開發(fā)微信端分享功能,結(jié)合圖文形式詳細(xì)分析了微信分享功能的原理、操作步驟及相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-04-04
  • flash+php+mysql打造簡單留言本教程

    flash+php+mysql打造簡單留言本教程

    剛開始做這個留言本的時候,連mysql和php都沒接觸過。經(jīng)過痛苦的查找資料和學(xué)習(xí),郁悶了一個星期后完成了我的簡單留言本
    2008-07-07

最新評論

清水河县| 定陶县| 贞丰县| 文登市| 图片| 蓬溪县| 新营市| 清新县| 积石山| 和静县| 盐亭县| 清涧县| 工布江达县| 泰兴市| 洛浦县| 亳州市| 灵川县| 通山县| 鹰潭市| 麻栗坡县| 大兴区| 河东区| 南充市| 洛宁县| 瑞昌市| 库伦旗| 合作市| 尉氏县| 鹿泉市| 黄冈市| 台安县| 南川市| 怀远县| 荥经县| 沙雅县| 济阳县| 延长县| 娄底市| 鄂温| 邹平县| 黄龙县|