时间:2021-07-01 10:21:17 帮助过:24人阅读
主要角色
抽象责任(Responsibility)角色:定义所有责任支持的公共方法。
具体责任(Concrete Responsibility)角色:以抽象责任接口实现的具体责任
责任链(Chain of responsibility)角色:设定责任的调用规则
类图

实例
<?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();
}
};
}
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);
?>