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 to set the next responsibility object ** @ param object $ objsuccessor */public function setSucce Ssor ($ objsuccessor) {$ this-> successor = $ 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 has no other. Then the client passes the request to the first processor object.