Responsibility chain mode and php implementation

Source: Internet
Author: User
The responsibility chain mode and php implementation of the responsibility chain mode (also called the responsibility chain mode ):
It contains some command objects and some processing objects. Each processing object determines which command objects it can process. it also knows that it should deliver the command objects that it cannot process to the next processing object, this mode also describes how to add a new object to the chain.

Role:
Abstract handler: defines an interface for processing requests. If necessary, the interface can define a method to set and return references to the next house. This role is usually implemented by an abstract class or interface.
Specific handler: After receiving the request, the handler can choose to process the request or send the request to the next home. Because the specific handler holds a reference to the next home, if necessary, the specific handler can access the next home.

UML class diagram:

     

Applicable scenarios:
1. multiple objects can process the same request. the runtime determines which object will process the request automatically.
2. if the receiver is not explicitly specified, submit a request to one of multiple objects.
3. a group of objects can be dynamically specified to process requests.

A simple summary of the responsibility chain model can be summarized:
A series of classes (classes) are used to process a request. These classes are loosely coupled and the only thing in common is to pass requests between them. that is to say, when A request comes, Class A First processes it. if it is not processed, it is passed to Class B for processing. if it is not processed, it is passed to class C for processing, it is passed down like a chain.

Code implementation:

 Name = $ name; $ this-> requestContent = $ requestContent; $ this-> reason = $ reason; $ this-> num = $ num ;}// abstract processing class, define the public attributes of the handler and the interfaces to be implemented by the specific processing class abstract class AbstractManager {protected $ name; // name protected $ position; // position protected $ head; // superior function _ construct ($ name, $ position) {$ this-> name = $ name; $ this-> position = $ position ;} // Set the supervisor function setHead (AbstractManager $ head) {$ this-> head = $ head ;} // Request processing abstract function handleRequest (request $ Request);} // specific processing class, manager, class ctor extends AbstractManager {function _ construct ($ name, $ position) {parent :: __construct ($ name, $ position);} // process the application function handleRequest (Request $ request) {if ($ request-> requestContent = "leave" & $ request-> num <7) | ($ request-> requestContent = "salary increase" & $ request-> num <1000) {return $ request-> nam E. ". $ request-> requestContent. "The request has been ". $ this-> name. "approval. ";} else {// transfer the processing permission to the superior director for processing if (isset ($ this-> head )) {return $ this-> head-> handleRequest ($ request) ;}}// specific processing class, Director, class Majordomo extends AbstractManager {function _ construct ($ name, $ position) {parent :: __construct ($ name, $ position);} // process the application function handleRequest (Request $ request) {if ($ request-> requestContent = "leave" & $ Request-> num <15) | ($ request-> requestContent = "salary increase" & $ request-> num <2000 )) {return $ request-> name. ". $ request-> requestContent. "The request has been ". $ this-> name. "approval. ";} else {// transfer the processing permission to the upper-level general manager for processing if (isset ($ this-> head )) {return $ this-> head-> handleRequest ($ request) ;}}// specific processing class, general manager, class GeneralManager extends AbstractManager {function _ construct ($ name, $ position) {parent ::__ construct ($ name, $ posi Tion);} // process the Request function handleRequest (request $ request) {if ($ request-> requestContent = "" & $ Request-> num <30) | ($ request-> requestContent = "salary increase" & $ request-> num <5000) {return $ request-> name. ". $ request-> requestContent. "The request has been ". $ this-> name. "approval. ";} else {// no superior return $ request-> name. ". $ request-> requestContent. "The request has been ". $ this-> name. "No! ";}}// Test $ generalManagerWang = new GeneralManager (" Wang Wu "," General Manager "); $ majordomoLi = new Majordomo (" Li Si "," director "); $ majordomoLi-> setHead ($ generalManagerWang); $ directZhang = new ctor ("James", "manager"); $ directZhang-> setHead ($ majordomoLi ); $ request = new Request ("Ye Liangchen", "ask for leave", "If you have any questions, please feel free to contact me. Liang Chen prefers to sell to self-righteous people", 100 ); $ result = $ directZhang-> handleRequest ($ request); // Ye Liangchen's leave request has been rejected by Wang Wu! Echo $ result;

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.