<?php/** * Responsibility Chain Mode * * to unbind the sender and receiver of the request, and use multiple objects to handle the request, link the objects together and pass the request along this chain until there is an object that handles it * Abstract processor role: Defines an interface for processing requests. and a successor (optional) specific processor role: handles the request it is responsible for, can access the successor, and if it can handle the request, otherwise forwards the request to his successor. Customer class: Submits a request to a specific processor on a chain Concretehandler object. * */* Responsibility chain mode is relatively flexible, the chain can be set to straight, ring can be * * Also have a pure responsibility chain, impure responsibility chain *///abstract processor role, generally contains two methods: processing the request, set the successor abstract class Handler{public for the request $next _handler;public function Setnexthandler ($handler) {$this->next_handler = $handler;} Abstract public Function executerequest ($request);} The specific processor, if able to handle, handle itself, cannot handle, leaving the next successor class Leader extends Handler{public function executerequest ($request) {if ($request- >days>0&& $request->days<=1) {echo ' team leader has approved ';} else{$this->next_handler->executerequest ($request);}} Class Director extends Handler{public function executerequest ($request) {if ($request->days>1&&$ request->days<=3) {echo ' director has approved ';} else{$this->next_handler->executerequest ($request);}} Class Manager extends Handler{public function executerequest ($requeST) {if ($request->days>3) {echo ' general manager has approved ';} else{$this->next_handler->executerequest ($request);}} Request Class Request{public $days;p ublic function __construct ($days) {$this->days = $days;}} Class Client{public static function Main1 () {//Constructs a request, three processing roles $request = new Request (2), $leadler = new Leader (); $director = n EW Director (); $manager = new manager ();//Make the three chains $leadler->setnexthandler ($director); $director->setnexthandler ($manager);//The chain head makes a request $leadler->executerequest ($request);} Public Function main2 () {$request = new request (2), $leadler = new Leader (); $director = new Director (); $manager = new Manage R ();//form a ring chain, who can be the recipient of $leadler->setnexthandler ($director); $director->setnexthandler ($manager); $ Manager->setnexthandler ($leadler); $manager->executerequest ($request);}} Client::main2 ();? >
The UML class diagram is as follows:
PHP's responsibility chain model for implementing design patterns