summary
Definition of Chain of Responsibility mode: in order to avoid coupling the request sender with multiple request handlers, all request handlers are connected into a chain by remembering the reference of the next object through the previous object; When a request occurs, it can be passed along the chain until an object processes it.
Pattern structure
Take the example code of this article as a reference
- Leader - leader class
- GroupLeader - group leader class
- Department - department head
- factoryDirector - factory director
UML legend
Code example
<?php //Leader class abstract class Leader{ private $leader; //Set up next level administrator public function setNext(Leader $leader){ $this->leader = $leader; } //Get current administrator public function getNext(){ return $this->leader; } //Processing subordinate leave requests public abstract function handleRequest($leaveDays); } //Group leader class GroupLeader extends Leader{ //For holidays within one day, the team leader has authority private $leaveDays = 1; public function handleRequest($leaveDays){ if($leaveDays > $this->leaveDays){ echo "leave ".$this->leaveDays." More than days shall be approved by the superior department<br>" ; } else{ echo "Your request for leave ".$leaveDays." Day was approved by the team leader<br>"; exit(); } } } //Department head class Department extends Leader{ //The vacation team leader within five days has authority private $leaveDays = 5; public function handleRequest($leaveDays){ if($leaveDays > $this->leaveDays){ echo "leave ".$this->leaveDays." More than days shall be approved by the superior department<br>" ; } else{ echo "Your request for leave ".$leaveDays." The day was approved by the department head<br>"; exit(); } } } class factoryDirector extends Leader{ //The vacation team leader within five days has authority private $leaveDays = 30; public function handleRequest($leaveDays){ if($leaveDays > $this->leaveDays){ echo "leave ".$this->leaveDays." More than days shall be approved by the superior department<br>" ; } else{ echo "Your request for leave ".$leaveDays." The factory director approved it<br>"; exit(); } } } //Leave days $leaveDays = 10; $groupLeader = new GroupLeader(); $department = new Department(); $factoryDirector = new factoryDirector(); $groupLeader->handleRequest($leaveDays); $groupLeader->setNext($department); $next = $groupLeader->getNext(); $next->handleRequest($leaveDays); $groupLeader->setNext($factoryDirector); $next = $groupLeader->getNext(); $next->handleRequest($leaveDays);
pattern analysis
In the responsibility chain mode, the customer only needs to send the request to the responsibility chain, and does not need to care about the processing details of the request and the transmission process of the request. The request will be transmitted automatically. Therefore, the responsibility chain decouples the sender of the request from the handler of the request.
Advantages and disadvantages
Advantages of responsibility chain model
- Reduces the coupling between objects. This mode makes an object do not need to know which object handles its request and the structure of the chain, and the sender and receiver do not need to have each other's clear information.
- It enhances the scalability of the system. New request processing classes can be added as needed to meet the opening and closing principle.
- Increased flexibility in assigning responsibilities to objects. When the workflow changes, you can dynamically change the members in the chain or transfer their order, or dynamically add or delete responsibilities.
- The chain of responsibility simplifies the connection between objects. Each object only needs to maintain a reference to its successor without maintaining the references of all other processors, which avoids the use of many if or if ·· else statements.
- Responsibility sharing. Each class only needs to deal with its own work that should be handled, and the work that should not be handled should be transferred to the next object for completion. The scope of responsibility of each class should be defined and in line with the principle of single responsibility of the class.
Disadvantages of responsibility chain model
- There is no guarantee that every request will be processed. Since a request has no specific receiver, it cannot be guaranteed that it will be processed. The request may not be processed until it reaches the end of the chain.
- For a long responsibility chain, the processing of requests may involve multiple processing objects, and the system performance will be affected to some extent.
- The rationality of the establishment of responsibility chain depends on the client, which increases the complexity of the client, and may lead to system errors due to the wrong setting of responsibility chain, such as circular call.
Applicable environment
The structure and characteristics of the responsibility chain mode have been described earlier. The application scenarios are introduced below. The responsibility chain mode is usually used in the following situations.
- Multiple objects can process a request, but which object handles the request is automatically determined at run time.
- You can dynamically specify a set of objects to process requests, or add new processors.
- You need to submit a request to one of multiple processors without explicitly specifying the request processor.