A brief introduction to PHP's responsibility chain programming model _php Skills

Source: Internet
Author: User

Overview
The responsibility chain pattern is an object's behavior pattern. In the mode of responsibility chain, many objects are connected by each object's reference to their house to form a chain. The request is passed on this chain until an object on the chain decides to process the request. The client issuing the request does not know which object on the chain ultimately processes the request, allowing the system to dynamically rearrange and distribute the responsibility without affecting the client

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

the advantages of the responsibility chain model
the most notable advantage is the separation of requests from processing. The requester can not know who is handling, the processor can not know the full picture of the request, both decoupling, improve the flexibility of the system.

the drawback of the responsibility chain model
first, the performance problem, each request is from the chain head to the end of the chain, especially in the chain is relatively long, performance is a problem. Second, debugging is not very convenient, especially the chain is relatively long, links are more often, because of the use of similar recursion, debugging 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 references to the home. This role is typically implemented by an abstract class or interface of PHP. The aggregation relation of the handler class in the above figure gives the reference of the specific subclass to the following, and the abstract Method HandleRequest () regulates the operation of the subclass processing request.
Specific processor (Concreatehandle) role: When a specific processor receives a request, he or she can choose to dispose of the request or pass the request to the other person. Because the specific processor holds a reference to the homes, therefore, if necessary, specific processing this can visit the under


To see a PHP responsibility Chain mode Programming Example:

  <?php/** * Abstract Processor Role * @author wzy * * */abstract class Handle {/** * hold 
   
    The subject of responsibility * * @var object/protected $successor; /** * Signals the method of processing the request, although this schematic method is not passed into the parameters of the * but actually can pass the parameter, according to the specific needs to choose whether to pass the parameters/public abstract function Handlere 
   
    Quest (); /** * Value Method * @return Object */Public function getsuccessor () {return $this-& 
    Gt;successor; /** * Assign value method, set successor responsibility Object * * @param object $objsuccessor/Public function SetS 
    Uccessor ($objsuccessor) {$this->successor = $objsuccessor; 
   
    }/** * Specific processor role * * @author wzy * */class Concreatehandler extends Handle { 
     /** * To determine whether there is a successor to the subject of responsibility * if any, forward the request to the successor of responsibility * if not, process request * * @see handle::handlerequest () */Public Function HandleRequest () {if ($this->getsuccessor ()!= null) {echo "Pass the request, forward the request to the successor responsible object!<br>"; 
      $this->getsuccessor ()->handlerequest (); 
      else {echo handles the request, the process omits ...<br>; 
  /** * Client code//assembly RESPONSIBILITY chain $handle 1 = new Concreatehandler (); 
  $handle 2 = new Concreatehandler (); 
   
  $handle 1->setsuccessor ($handle 2); 
  Submit Request $handle 1->handlerequest (); 
 ?>


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

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.