PHP Responsibility Chain programming model

Source: Internet
Author: User
This paper mainly introduces the responsibility chain programming mode of PHP, and it is also a kind of programming agreement widely adopted by the development team. We hope to help you.

Overview
The responsibility chain pattern is an object's behavior pattern. In the chain of responsibility, many objects are linked together by each object's reference to its next generation. The request is passed on this chain until an object on the chain decides to process the request. The client making this request does not know which object on the chain is ultimately processing the request, which allows the system to reorganize and assign responsibilities dynamically without impacting the client


Definition of the responsibility chain model
Enables multiple objects to have the opportunity to process requests, thereby avoiding the coupling between the sender and the recipient of the request, connecting the objects to a chain, and passing the request along the chain until an object has processed it.

The advantages of the responsibility chain model
The most significant advantage is separating the request from the processing. The requester can not know who is dealing with, the processor can not know the full picture of the request, the two decoupling, improve the system flexibility.

Disadvantages of the responsibility chain model
One is performance problems, and each request is traversed from the chain head to the end of the chain, especially when the chain is longer, performance is a problem. Second, debugging is not very convenient, especially the chain is longer, the link is more, because the use of a similar recursive way, debugging when the logic may be more complex.


The responsibility chain model involves the following roles:

Abstract processor (Handler) role: 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 a PHP abstract class or interface. The aggregation relationship of handler class gives a reference to the other classes, and the abstract Method HandleRequest () regulates the operation of the subclass processing request
Specific processor (Concreatehandle) role: When a specific processor receives a request, it can choose to dispose of the request or pass the request to the other person. As the specific processor holds a reference to the homes, if necessary, the specific handling of this can visit the


Look at a PHP responsibility chain pattern Programming Example:

  <?php/** * Abstract Processor Role * @author wzy * * * */abstract class Handle {/** * Hold successor Responsible Object *        * @var Object */protected $successor; /** * Signals the method of processing the request, although the schematic method is not passed in the parameter * but actually can be passed in the parameters, according to the specific needs to choose whether to pass the parameter */public abstract function Handlereques        T (); /** * Value Method * * @return Object * * Public Function Getsuccessor () {return $this-&GT;SUCC     Essor; }/** * Assignment method, set subsequent responsibility object * * @param object $objsuccessor */Public function Setsuccesso     R ($objsuccessor) {$this->successor = $objsuccessor;      }}/** * Specific processor role * * @author wzy * * */class Concreatehandler extends Handle {/**  * To determine if there is a successor to the object of responsibility * if there is, forward the request to the successor of the responsible object * if not, then process the request * * @see handle::handlerequest () */Public function HandleRequest () {if ($this->getsuccessor () = null) {echo] misses the request and forwards the request to the successor responsible object!<bR> ";       $this->getsuccessor ()->handlerequest ();       } else {echo "processing the request, the process omitted ...<br>";   }}}/** * Client code *///Assemble responsibility chain $handle 1 = new Concreatehandler ();   $handle 2 = new Concreatehandler ();      $handle 1->setsuccessor ($handle 2);   Submit a Request $handle 1->handlerequest (); ?>

As you can see from the code, the client creates two processor objects and specifies that the first handler object is the second handler object, while the second handler object does not have the next one. The client then passes the request to the first handler object

Related recommendations:

PHP State Mode Programming

PHP single-instance mode implementation

PHP Factory Method mode

Related Article

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.