PHP design mode-responsibility chain mode. PHP design pattern-responsibility chain pattern the responsibility chain pattern (also called the responsibility chain pattern) contains some command objects and some processing objects, each processing object determines that it can process those command objects. PHP design mode-responsibility chain mode
The responsibility chain mode (also called the responsibility chain mode) contains some command objects and some processing objects. Each processing object determines that it can process those command objects, it also knows that the command object that cannot be processed should be handed over to a processing object. This mode also describes how to add a new processing object to the chain.
UML class diagram:
Role:
Abstract handler (Manager): 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.
CommonManager: After receiving a request, the processor can either process the request or send the request to another user. Because the specific handler holds a reference to the next home, if necessary, the specific handler can access the next home.
Core code:
Name = $ _ name;} // Set the administrator's upper-level public function SetHeader (Manager $ _ mana) {$ this-> manager =$ _ mana ;} // Request abstract public function Apply (Request $ _ req);} // Manager class CommonManager extends Manager {public function _ construct ($ _ name) {parent :: __construct ($ _ name);} public function Apply (Request $ _ req) {if ($ _ req-> requestType = leave & $ _ req-> num <= 2) {echo {$ this-> name }: {$ _ req-> requestContent} quantity {$ _ req-> n Um} is approved .;} Else {if (isset ($ this-> manager) {$ this-> manager-> Apply ($ _ req );}}}} // director class MajorDomo extends Manager {public function _ construct ($ _ name) {parent ::__ construct ($ _ name);} public function Apply (Request $ _ req) {if ($ _ req-> requestType = leave & $ _ req-> num <= 5) {echo {$ this-> name }: the number of {$ _ req-> requestContent} {$ _ req-> num} is approved .;} Else {if (isset ($ this-> manager) {$ this-> manager-> Apply ($ _ req );}}}} // general Manager class GeneralManager extends Manager {public function _ construct ($ _ name) {parent ::__ construct ($ _ name);} public function Apply (Request $ _ req) {if ($ _ req-> requestType = leave) {echo {$ this-> name }: the number of {$ _ req-> requestContent} {$ _ req-> num} is approved .;} Else if ($ _ req-> requestType = salary increase & $ _ req-> num <= 500) {echo {$ this-> name }: the number of {$ _ req-> requestContent} {$ _ req-> num} is approved .;} Else if ($ _ req-> requestType = salary increase & $ _ req-> num> 500) {echo {$ this-> name }: {$ _ req-> requestContent} quantity {$ _ req-> num .; }}}
Call the client code:
Header (Content-Type: text/html; charset = utf-8); // -------------------- responsibility chain mode ---------------------- require_once. /Responsibility. php; $ jingli = new CommonManager (manager Li); $ zongjian = new MajorDomo (director Guo); $ zongjingli = new GeneralManager (Sun Zong ); // set the upper-level $ jingli-> SetHeader ($ zongjian); $ zongjian-> SetHeader ($ zongjingli); // apply for $ req1 = new Request (); $ req1-> requestType = ask for leave; $ req1-> requestContent = ask for leave !; $ Req1-> num = 1; $ jingli-> Apply ($ req1); $ req2 = new Request (); $ req2-> requestType = leave; $ req2-> requestContent = side dish for leave !; $ Req2-> num = 4; $ jingli-> Apply ($ req2); $ req3 = new Request (); $ req3-> requestType = salary increase; $ req3-> requestContent = small Dish request a raise !; $ Req3-> num = 500; $ jingli-> Apply ($ req3); $ req4 = new Request (); $ req4-> requestType = salary increase; $ req4-> requestContent = small Dish request a raise !; $ Req4-> num = 1000; $ jingli-> Apply ($ req4 );
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.
So far, PHP design pattern series tutorials have all been updated. You are welcome to criticize and correct them. Your words are the motivation for me to move forward.
The worker responsibility chain mode (also called the responsibility chain mode) contains some command objects and some processing objects. Each processing object determines which command objects can be processed...