当前位置:网站首页>Responsibility chain model of behavioral model
Responsibility chain model of behavioral model
2022-07-25 23:57:00 【w ͏ l ͏ j ͏】
The chain of responsibility model
What is it? ?
Responsibility chain (Chain of Responsibility) Pattern : In order to avoid coupling the request sender with multiple request processors , Thus, all the request processors are connected in a chain by remembering the reference of the next object by the previous object ; When a request occurs , The request can be passed along this chain , Until an object processes it . There are similar examples in life , For example, purchase approval process 、 Leave process, etc ; It is also very common in computer software and hardware , Like interceptors , filter , Can consider the responsibility chain model to achieve .
In the responsibility chain model , The customer just needs to send the request to the chain of responsibility , Don't care about the processing details of the request and the delivery process of the request , The request is automatically delivered . So the chain of responsibility decouples the sender of the request from the handler of the request .
advantage
- It reduces the coupling between objects . This pattern makes it unnecessary for an object to know which object handles its request and the structure of the chain , Neither the sender nor the receiver has to have the other party's explicit information .
- Enhanced system scalability . You can add new request processing classes as needed , It satisfies the open close principle .‘
- Increased flexibility in assigning responsibilities to objects . When the workflow changes , Can dynamically change the members of the chain or transfer their order , You can also dynamically add or delete responsibilities .
- The chain of responsibility simplifies the connection between objects . Each object only needs to keep a reference to its successors , No need to keep references from all other processors .
- Share responsibility . Each class only needs to handle its own work , What should not be processed is passed to the next object to complete , Clarify the scope of responsibility of all kinds , According to the single responsibility principle of class .
shortcoming
- There is no guarantee that every request will be processed . Because a request has no clear recipient , So there's no guarantee that it will be dealt with , The request may not be processed all the way to the end of the chain .
- For a longer responsibility chain , The processing of the request may involve multiple processing objects , System performance will be affected ,
- The rationality of responsibility chain establishment depends on the client to ensure , Increased client complexity , System error may be caused by wrong setting of responsibility chain , If it is possible to cause a circular call .
The structure of the pattern


Realization
Use the leave approval module to simply simulate the application of the responsibility chain mode .
scene : Students ask for leave
Students ask for leave less than 2 God , The head teacher can directly approve ; Students ask for leave less than 7 God , The dean of the Department can directly approve ; Less than 10 God , The president can approve , The rest will not be approved .
Code implementation
Headmaster , The dean of the Department , dean
package com.pattern.chainOfResponsibility;
/** * @author wlj * @Classname Leader * @Description Abstract leadership * @Date 7/24/2022 10:35 PM */
public abstract class Leader {
// The next citation of responsibility
private Leader next;
public Leader getNext() {
return next;
}
public void setNext(Leader next) {
this.next = next;
}
/** * Deal with students' leave requests * @param day The number of days students ask for leave */
public abstract void handleRequest(int day);
}
// Headmaster
package com.pattern.chainOfResponsibility;
/** * @author wlj * @Classname ClassAdviser * @Description Headmaster * @Date 7/24/2022 10:42 PM */
public class ClassAdviser extends Leader{
@Override
public void handleRequest(int day) {
if(day <= 2){
System.out.println(" Approved by the head teacher :"+day);
}else{
if(getNext() != null){
getNext().handleRequest(day);
}
else{
System.out.println(" Too many days off , Not approved ");
}
}
}
}
// The dean of the Department
package com.pattern.chainOfResponsibility;
/** * @author wlj * @Classname ClassAdviser * @Description The dean of the Department * @Date 7/24/2022 10:42 PM */
public class DepartmentHead extends Leader{
@Override
public void handleRequest(int day) {
if(day <= 7){
System.out.println(" Approved by the Dean :"+day);
}else{
if(getNext() != null){
getNext().handleRequest(day);
}
else{
System.out.println(" Too many days off , Not approved ");
}
}
}
}
// dean
package com.pattern.chainOfResponsibility;
/** * @author wlj * @Classname ClassAdviser * @Description dean * @Date 7/24/2022 10:42 PM */
public class Dean extends Leader{
@Override
public void handleRequest(int day) {
if(day <= 10){
System.out.println(" Approved by the president :"+day);
}else{
if(getNext() != null){
getNext().handleRequest(day);
}
else{
System.out.println(" Too many days off , Not approved ");
}
}
}
}
client
package com.pattern.chainOfResponsibility;
/** * @author wlj * @Classname Client * @Description Scene simulation * @Date 7/24/2022 10:51 PM */
public class Client {
public static void main(String[] args) {
// Build a chain of responsibility
Leader leader1 = new ClassAdviser();
Leader leader2 = new DepartmentHead();
Leader leader3 = new Dean();
// leader1(ClassAdviser) -> leader2(DepartmentHead) ->leader3(Dean)
leader1.setNext(leader2);
leader2.setNext(leader3);
leader3.setNext(null);
// Simulated leave 2 God
leader1.handleRequest(2);
// Simulated leave 8 God
leader1.handleRequest(7);
// Simulated leave 9 God
leader1.handleRequest(9);
// Simulated leave 12 God
leader1.handleRequest(12);
}
}
output:
Approved by the head teacher :2
Approved by the Dean :7
Approved by the president :9
Too many days off , Not approved
边栏推荐
- 回溯——77. 组合
- Programmer interview Golden Classic 4.12 summation path
- Find the cause of program dead cycle through debugging
- 二叉树——654. 最大二叉树
- 下一代终端安全管理的关键特征与应用趋势
- Payment terms in SAP message No. vg202 IDoc e1edk18 have been transferred: check data
- 程序员面试金典 4.12 求和路径
- Numerical learning iota, accumulate
- 【MUDUO】EventLoopThreadPool
- Demo of pointer function
猜你喜欢
随机推荐
Demo of pointer function
[intelligence question] interview intelligence question
模式之固定与交替顺序执行
二叉树——700.二叉搜索树中的搜索
Nacos 下线服务时报错 errCode: 500
Fixed and alternate sequential execution of modes
The late Apple co-founder Steve Jobs was posthumously awarded the U.S. presidential medal of freedom
Scroll series
Interview focus - TCP protocol of transport layer
How does JS judge whether the current date is within a certain range
Several ways of writing strings in reverse order
二叉树——110. 平衡二叉树
下一代终端安全管理的关键特征与应用趋势
调用钉钉api报错:机器人发送签名过期;solution:签名生成时间和发送时间请保持在 timestampms 以内
二叉树——617. 合并二叉树
Good news under the epidemic
静态代理+动态代理
The process of finding free screen recording software - I didn't expect win10 to come with this function
意向不到的Dubug妙招
initializer_ List tool library learning




![[learning notes] solid works operation record](/img/f7/0535b473755643ce32996f00fa5554.png)




