這種模式有另外一種稱呼:控制鏈模式。它主要由一系列對於某些命令的處理器構成,每一個查詢會在處理器構成的責任鏈中傳遞,在每一個交匯點由處理器判斷是否須要對它們進行響應與處理。每次的處理程序會在有處理器處理這些請求時暫停。php
<?php abstract class Responsibility { // 抽象責任角色 protected $next; // 下一個責任角色 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(); echo 'Res_A start'."<br>"; } } } class ResponsibilityB extends Responsibility { public function __construct() {} public function operate(){ if (false == is_null($this->next)) { $this->next->operate(); echo 'Res_B start'; } } } $res_a = new ResponsibilityA(); $res_b = new ResponsibilityB(); $res_a->setNext($res_b); $res_a->operate();//輸出:Res_A start