常用設(shè)計(jì)模式之責(zé)任鏈模式及其PHP實(shí)現(xiàn)
責(zé)任鏈模式是一種行為型模式,它包含了一些命令對(duì)象和一系列的處理對(duì)象。每一個(gè)處理對(duì)象決定它能處理哪些命令對(duì)象,它也知道如何將它不能處理的命令對(duì)象傳遞給該鏈中的下一個(gè)處理對(duì)象。該模式還描述了往該處理鏈的末尾添加新的處理對(duì)象的方法。
主要角色抽象責(zé)任(Responsibility)角色:定義所有責(zé)任支持的公共方法。具體責(zé)任(Concrete Responsibility)角色:以抽象責(zé)任接口實(shí)現(xiàn)的具體責(zé)任責(zé)任鏈(Chain of responsibility)角色:設(shè)定責(zé)任的調(diào)用規(guī)則類圖<?phpabstract class Responsibility { // 抽象責(zé)任角色 protected $next; // 下一個(gè)責(zé)任角色 public function setNext(Responsibility $l) {$this->next = $l;return $this; } abstract public function operate(); // 操作方法} class ResponsibilityA extends Responsibility { public function __construct() {} public function operate(){if (false == is_null($this->next)) { $this->next->operate();} };}class ResponsibilityB extends Responsibility { public function __construct() {} public function operate(){if (false == is_null($this->next)) { $this->next->operate();} };} $res_a = new ResponsibilityA();$res_b = new ResponsibilityB();$res_a->setNext($res_b);?>
相關(guān)文章:
1. java——Byte類/包裝類的使用說(shuō)明2. android studio實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器(無(wú)bug)3. python 讀txt文件,按‘,’分割每行數(shù)據(jù)操作4. python Selenium 庫(kù)的使用技巧5. android 控件同時(shí)監(jiān)聽(tīng)單擊和雙擊實(shí)例6. python+pywinauto+lackey實(shí)現(xiàn)PC端exe自動(dòng)化的示例代碼7. vue使用exif獲取圖片經(jīng)緯度的示例代碼8. python logging.info在終端沒(méi)輸出的解決9. 詳解android adb常見(jiàn)用法10. Python 忽略文件名編碼的方法
