职责链模式:
使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。
UML:
Client:客户端
Handler: 抽象处理者:定义出一个处理请求的接口。如果需要,接口可以定义出一个方法以设定和返回对下家的引用。这个角色通常由一个抽象类或接口实现。
ConcreteHandler:具体处理者:具体处理者接到请求后,可以选择将请求处理掉,或者将请求传给下家。由于具体处理者持有对下家的引用,因此,如果需要具体处理者可以访问下家。
代码:
successor)) { $this->successor = $handler; //添加下一个链 } else { $this->successor->append($handler); //有下一个链再为下一个链添加链 } } final public function handle(Request $req) { //处理请求的方法 $req->forDebugOnly = get_called_class(); //判断具体是哪一个类调用,跟责任链模式无关。 $processed = $this->processing($req); if (!$processed) { // the request has not been processed by this handler => see the next if (!is_null($this->successor)) { $processed = $this->successor->handle($req); } } return $processed; } abstract protected function processing(Request $req);}class FastStorage extends Handler { //快速处理数据类 protected $data = array(); public function __construct($data = array()) { $this->data = $data; } protected function processing(Request $req) { if ('get' === $req->action) { //Request传入的查询模式,例子中都是get if (array_key_exists($req->key, $this->data)) { // 处理器可响应,于是返回数据 $req->response = $this->data[$req->key]; return true; } } return false; }}class SlowStorage extends Handler { //慢速处理数据类 protected $data = array(); public function __construct($data = array()) { $this->data = $data; } protected function processing(Request $req) { if ('get' === $req->action) { if (array_key_exists($req->key, $this->data)) { $req->response = $this->data[$req->key]; return true; } } return false; }}$slow = new SlowStorage(array('20141018' => 'happy', '20141019' => 'lucky')); //先存储一些数据//$slow->append(new MoreSlowStorate());$storageChain = new FastStorage(array('20141018' => 'happy')); //先存储一些数据,比慢存贮少一个数据,为了测试$storageChain->append($slow); //把慢存储添加为快存贮的一个链条$request = new Request(); //发起请求$request->action = 'get';$request->key = '20141018';$result = $storageChain->handle($request); //这个在快存贮了直接找到了,返回true表示。echo $request->response."\n"; //happyecho $request->forDebugOnly."\n"; //FastStorage$request->key = '20141019'; //在发起一个快存贮没有的数据请求,快存贮会找下一个链来处理$result = $storageChain->handle($request);echo $request->response."\n"; //luckyecho $request->forDebugOnly."\n"; //SlowStorage
总结:
优点:
降低耦合度
可简化对象的相互连接
增强给对象指派职责的灵活性
方便增加新的类
缺点:
请求到达末端也可能得不到处理
代码调试时不太方便,可能会造成循环调用
使用场景:
有多个对象可以处理同一个请求,具体哪个对象处理该请求由运行时刻自动确定
在不明确指定接受者的情况下,向多个对象中的一个提交一个请求
可动态指定一组对象处理请求