成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

PHP設計模式漫談之責任鏈模式

原創
開發 后端 前端
51CTO將帶您繼續深入PHP設計模式,講解的行為模式是責任鏈模式,其目的是組織一個對象鏈處理一個如方法調用的請求。

【51CTO獨家特稿】在上期的《PHP設計模式漫談》中,我們講到了PHP的代理模式,本節我們將繼續深入PHP設計模式,講解的行為模式是責任鏈模式,其目的是組織一個對象鏈處理一個如方法調用的請求。

當ConcreteHandler(具體的處理程序)不知道如何滿足來自Client的請求時,或它的目的不是這個時,它會委派給鏈中的下一個Handler(處理程序)來處理。
這個設計模式通常和復合模式一起使用,其中有些葉子或容器對象默認委派操作給它們的父對象。另一個例子是,本地化通常是使用責任鏈處理的,當德語翻譯適配器沒有為翻譯關鍵詞找到合適的結果時,就返回到英語適配器或干脆直接顯示關鍵詞本身。

耦合減少到最低限度:Client類不知道由哪個具體的類來處理請求;在創建對象圖時配置了鏈;ConcreteHandlers不知道哪個對象是它們的繼承者。行為在對象之間分配是成功的,鏈中最近的對象有優先權和責任滿足請求。

PHP設計模式中的責任鏈模式 
PHP設計模式中的責任鏈模式

參與者:

◆Client(客戶端):向Handler(處理程序)提交一個請求;

◆Handler(處理程序)抽象:接收一個請求,以某種方式滿足它;

◆ConcreteHandlers(具體的處理程序):接收一個請求,設法滿足它,如果不成功就委派給下一個處理程序。

下面的代碼實現了一個最著名的責任鏈示例:多級緩存。

  1. /**  
  2.  * The Handler abstraction. Objects that want to be a part of the  
  3.  * ChainOfResponsibility must implement this interface directly or via  
  4.  * inheritance from an AbstractHandler.  
  5.  */ 
  6. interface KeyValueStore  
  7. {  
  8.     /**  
  9.      * Obtain a value.  
  10.      * @param string $key  
  11.      * @return mixed  
  12.      */ 
  13.     public function get($key);  
  14. }  
  15.  
  16. /**  
  17.  * Basic no-op implementation which ConcreteHandlers not interested in  
  18.  * caching or in interfering with the retrieval inherit from.  
  19.  */ 
  20. abstract class AbstractKeyValueStore implements KeyValueStore  
  21. {  
  22.     protected $_nextHandler;  
  23.  
  24.     public function get($key)  
  25.     {  
  26.         return $this->_nextHandler->get($key);  
  27.     }  
  28. }  
  29.  
  30. /**  
  31.  * Ideally the last ConcreteHandler in the chain. At least, if inserted in  
  32.  * a Chain it will be the last node to be called.  
  33.  */ 
  34. class SlowStore implements KeyValueStore  
  35. {  
  36.     /**  
  37.      * This could be a somewhat slow store: a database or a flat file.  
  38.      */ 
  39.     protected $_values;  
  40.  
  41.     public function __construct(array $values = array())  
  42.     {  
  43.         $this->_values = $values;  
  44.     }  
  45.  
  46.     public function get($key)  
  47.     {  
  48.         return $this->_values[$key];  
  49.     }  
  50. }  
  51.  
  52. /**  
  53.  * A ConcreteHandler that handles the request for a key by looking for it in  
  54.  * its own cache. Forwards to the next handler in case of cache miss.  
  55.  */ 
  56. class InMemoryKeyValueStore implements KeyValueStore  
  57. {  
  58.     protected $_nextHandler;  
  59.     protected $_cached = array();  
  60.  
  61.     public function __construct(KeyValueStore $nextHandler)  
  62.     {  
  63.         $this->_nextHandler = $nextHandler;  
  64.     }  
  65.  
  66.     protected function _load($key)  
  67.     {  
  68.         if (!isset($this->_cached[$key])) {  
  69.             $this->_cached[$key] = $this->_nextHandler->get($key);  
  70.         }  
  71.     }  
  72.  
  73.     public function get($key)  
  74.     {  
  75.         $this->_load($key);  
  76.         return $this->_cached[$key];  
  77.     }  
  78. }  
  79.  
  80. /**  
  81.  * A ConcreteHandler that delegates the request without trying to  
  82.  * understand it at all. It may be easier to use in the user interface  
  83.  * because it can specialize itself by defining methods that generates  
  84.  * html, or by addressing similar user interface concerns.  
  85.  * Some Clients see this object only as an instance of KeyValueStore  
  86.  * and do not care how it satisfy their requests, while other ones  
  87.  * may use it in its entirety (similar to a class-based adapter).  
  88.  * No client knows that a chain of Handlers exists.  
  89.  */ 
  90. class FrontEnd extends AbstractKeyValueStore  
  91. {  
  92.     public function __construct(KeyValueStore $nextHandler)  
  93.     {  
  94.         $this->_nextHandler = $nextHandler;  
  95.     }  
  96.  
  97.     public function getEscaped($key)  
  98.     {  
  99.         return htmlentities($this->get($key), ENT_NOQUOTES, 'UTF-8');  
  100.     }  
  101. }  
  102.  
  103. // Client code  
  104. $store = new SlowStore(array('pd' => 'Philip K. Dick',  
  105.                              'ia' => 'Isaac Asimov',  
  106.                              'ac' => 'Arthur C. Clarke',  
  107.                              'hh' => 'Helmut Heißenbüttel'));  
  108. // in development, we skip cache and pass $store directly to FrontEnd  
  109. $cache = new InMemoryKeyValueStore($store);  
  110. $frontEnd = new FrontEnd($cache);  
  111.  
  112. echo $frontEnd->get('ia'), "\n";  
  113. 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)一樣傳遞下去。

51CTO會在下周繼續深入PHP設計模式,講解PHP中的結構化模式摘要,敬請關注。

【編輯推薦】

  1. PHP設計模式漫談之代理模式
  2. 使用設計模式改善程序結構
  3. 架構、框架、設計模式之間的關系簡述
  4. 鐘勝輝談PHP發展的現狀和前景
  5. 揭示PHP成功背后的秘密:PHP創始人訪談錄 

原文:Practical Php Patterns: Chain of Responsibility    作者:Giorgio

鏈接:http://giorgiosironi.blogspot.com/2010/02/practical-php-patterns-chain-of.html

 

責任編輯:佚名 來源: 51CTO.com
相關推薦

2021-12-24 07:50:45

責任鏈模式設計

2010-03-25 08:52:30

PHP設計模式代理模式

2010-04-19 09:30:00

工廠模式PHP設計模式

2010-04-13 08:54:28

PHP設計模式命令模式

2010-04-08 09:27:04

PHP設計模式結構模式

2010-04-21 08:38:18

解釋器模式PHP設計模式

2010-04-29 08:53:11

PHP迭代器模式

2010-05-06 08:44:37

調解者模式

2012-03-28 13:28:56

Java設計模式

2023-06-05 07:55:31

2024-01-30 13:15:00

設計模式責任鏈

2020-11-17 09:32:57

設計模式責任鏈

2023-09-26 00:27:07

設計模式鏈接

2022-12-28 08:08:57

2021-08-14 08:17:49

Android設計模式OKHttp

2022-11-01 08:46:20

責任鏈模式對象

2024-05-09 12:17:00

責任鏈設計模式

2024-06-04 13:11:52

Python行為設計模式開發

2024-12-03 15:52:45

責任鏈Java

2021-07-14 10:08:30

責任鏈模式加工鏈
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩av在线一区 | 成人av免费 | 国产精品久久久久久久久久久久午夜片 | 亚洲视频在线观看 | 一级黄色裸片 | 欧美一级黄色免费看 | 日本不卡视频在线播放 | 91麻豆精品国产91久久久更新资源速度超快 | 国产精品免费视频一区 | 中文字幕一区二区三区在线观看 | 永久免费在线观看 | 7777精品伊人久久精品影视 | 国产精品国产三级国产aⅴ入口 | 国产一区二区三区色淫影院 | 亚洲成人精品 | 成人1区| 黄色大片免费看 | 国产免费又黄又爽又刺激蜜月al | 国产精品久久久久久久久久免费看 | 一区二区三区高清 | 亚洲成人av一区二区 | 欧美中文字幕一区二区三区 | 免费在线看黄 | 日韩免费一区二区 | av网站免费 | 久久久国产一区 | 欧美精品网站 | 精品综合久久久 | 欧美黑人狂野猛交老妇 | 欧美精品一区二区三区蜜桃视频 | 久久久黄色 | 欧美激情va永久在线播放 | 婷婷色成人 | 在线婷婷 | 日韩蜜桃视频 | 99热.com| 国产女人与拘做受免费视频 | 99精品视频免费在线观看 | 亚洲成人激情在线观看 | 丝袜美腿一区二区三区动态图 | 成人黄色在线 |