Brief Introduction to PHP's responsibility chain Programming Model

Source: Internet
Author: User
This article mainly introduces the PHP responsibility chain programming mode, which is also a programming method Convention widely used by the development team. If you need it, refer

This article mainly introduces the PHP responsibility chain programming mode, which is also a programming method Convention widely used by the development team. If you need it, refer

Overview
The responsibility chain mode is an object behavior mode. In the responsibility chain mode, many objects are connected by each object's reference to its next home to form a chain. Requests are transmitted on this chain until an object on the chain decides to process this request. The client that sends this request does not know which object on the chain will eventually process this request. This allows the system to dynamically reorganize and assign responsibilities without affecting the client.

Definition of the responsibility chain model
So that multiple objects have the opportunity to process the request, thus avoiding the coupling relationship between the request sender and the receiver, connecting these objects into a chain, and passing the request along this chain, until an object processes it.

Advantages of the responsibility chain model
The most significant advantage is separating requests from processing. The requester does not need to know who handled the request. The handler does not need to know the full picture of the request. The two are decoupled to improve the system flexibility.

Disadvantages of the responsibility chain model
First, performance problems. Each request is directed from the chain header to the end of the chain. Especially when the chain is long, performance is a problem. Second, debugging is not very convenient, especially the long chain. When there are many links, the logic may be complicated due to the recursive method.

The responsibility chain model involves the following roles:

Abstract Handler role: 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 of php. The aggregate relationship of the Handler class in gives reference to the sub-class. The abstract method handleRequest () standardizes the sub-class to process request operations.
ConcreateHandle: after receiving a request, the processor can process the request or send the request to the next user. Because the specific Handler holds a reference to the next house, if necessary, the specific handler can access the next house


Let's look at a PHP responsibility chain programming example:

<? Php/*** abstract processor role ** @ author wzy **/abstract class Handle {/*** holds the successor responsibility object ** @ var object */protected $ successor; /*** indicates the method used to process the request. Although this method does not include any input parameters *, the parameters can be passed in, select whether to pass the parameter */public abstract function handleRequest () as needed;/*** Value Method ** @ return object */public function getSuccessor () {return $ this-> successor;}/*** value assignment method, set the next responsibility object ** @ param object $ objsuccessor */public function setSuccessor ($ objsuccessor) {$ this-> succe Ssor = $ objsuccessor ;}} /*** specific processor role ** @ author wzy **/class ConcreateHandler extends Handle {/*** determine whether there are any successor responsibility objects * if there are, forward the request to the next responsible object * If not, process the request ** @ see Handle: handleRequest () */public function handleRequest () {if ($ this-> getSuccessor ()! = Null) {echo "let go of the request and forward the request to the next responsible object!
"; $ This-> getSuccessor ()-> handleRequest ();} else {echo" process the request. The process is omitted...
";}}/ ***** Client code * // assemble the responsibility chain $ handle1 = new ConcreateHandler (); $ handle2 = new ConcreateHandler (); $ handle1-> setSuccessor ($ handle2); // submit the request $ handle1-> handleRequest ();?>


The Code shows that the client creates two handler objects and specifies that the next of the first handler object is the second handler object, but the second handler object does not. Then the client passes the request to the first processor object.

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.