Responsibility chain model and PHP implementation

Source: Internet
Author: User

Responsibility chain mode (also called responsibility chain Mode):
Contains a number of command objects and some processing objects, each processing object determines that it can handle those command objects, it also knows that it should be unable to process the command object to the next processing object, the pattern also describes the way to add new processing objects to the chain.

Role:
Abstract Processor: Defines an interface for processing requests. If necessary, the interface can define a method to set and return a reference to the home. This role is usually implemented by an abstract class or interface.
Specific processor: When the specific processor receives the request, it can choose to dispose of the request or pass the request to the other person. As the specific processor holds references to the homes, the specific processor can access the homes if necessary.

UML Class Diagrams:

     

Applicable scenarios:
1. There are multiple objects that can handle the same request, and which object handles the request automatically determined by the run time.
2. Submit a request to one of several objects without explicitly specifying the recipient.
3. You can dynamically specify a set of object processing requests.

Simple summary of the responsibility chain model, can be summed up as:
Using a series of classes (classes) to try to handle a request for requests, these classes are loosely coupled, and the only common thing is to pass the request between them. That is, a request, a class first processing, if not processed, is passed to Class B processing, if not processed, passed to the Class C processing, just like a chain (chain) passed down.

Code implementation:

<?PHP//Application class,Header("content-type:text/html; Charset=utf-8 ");classrequest{ Public $name;  Public $requestContent;  Public $num;  Public $reason; function__construct ($name,$requestContent,$reason,$num){        $this->name =$name; $this->requestcontent =$requestContent; $this->reason =$reason; $this->num =$num; }}//An abstract processing class that defines the public properties that the processor has and the interfaces that the specific processing class needs to implementAbstract classabstractmanager{protected $name;//name    protected $position;//Job Title    protected $head;//boss    function__construct ($name,$position){        $this->name =$name; $this->position =$position; }     //set up boss    functionSethead (Abstractmanager$head){        $this->head =$head; }    //Processing Requests    Abstract functionHandleRequest (Request$request);}//The specific processing class, manager, can handle not more than 7 days of vacation and not more than 1000 yuan of raise requestclassDirectorextendsabstractmanager{function__construct ($name,$position) {Parent:: __construct ($name,$position); }        //Processing Requests    functionHandleRequest (Request$request){         if(($request->requestcontent = = "Leave" &&$request->num <7) | | ($request->requestcontent = = "Pay rise" &&$request->num <1000) ){              return $request->name. " of ".$request->requestcontent. " The request has been ".$this->name. " Approval. "; }Else{//exceeding the processing authority to the superior Director for processing            if(isset($this-head)) {                return $this->head->handlerequest ($request); }         }    }}//The specific processing class, the director, can handle no more than 15 days of leave and no more than $2000 for a raise requestclassMajordomoextendsabstractmanager{function__construct ($name,$position) {Parent:: __construct ($name,$position); }        //Processing Requests    functionHandleRequest (Request$request){         if(($request->requestcontent = = "Leave" &&$request->num <15) | | ($request->requestcontent = = "Pay rise" &&$request->num <2000) ){              return $request->name. " of ".$request->requestcontent. " The request has been ".$this->name. " Approval. "; }Else{//over processing permission to the Superior General manager to deal with            if(isset($this-head)) {                return $this->head->handlerequest ($request); }         }    }}//Specific handling classes, general manager, can handle leave and raise requestsclassGeneralmanagerextendsabstractmanager{function__construct ($name,$position) {Parent:: __construct ($name,$position); }        //Processing Requests    functionHandleRequest (Request$request){         if(($request->requestcontent = = "Leave" &&$request->num < 30) | | ($request->requestcontent = = "Pay rise" &&$request->num <5000) ){              return $request->name. " of ".$request->requestcontent. " The request has been ".$this->name. " Approval. "; }Else{//no superiors .            return $request->name. " of ".$request->requestcontent. " The request has been ".$this->name. " Veto! "; }    }}//Test$generalManagerWang=NewGeneralmanager ("Harry", "General manager");$majordomoLi=NewMajordomo ("John Doe", "director");$majordomoLi->sethead ($generalManagerWang);$directZhang=NewDirector ("Zhang San", "manager");$directZhang->sethead ($majordomoLi);$request=NewRequest ("Ye Liangchen", "Leave", "if you have a problem, you can always look for me, I like to the self-righteous people shot", 100);$result=$directZhang->handlerequest ($request);//Ye Liangchen's request for leave has been Harry rejected! Echo $result;

Responsibility chain model and PHP implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.