责任链设计模式(ChainOfResponsibility)

Posted by 余腾 on 2019-08-20
Estimated Reading Time 4 Minutes
Words 1.1k In Total
Viewed Times

什么是责任链设计模式?

责任链设计模式 —> 行为型模式

  • 职责链模式(Chain of Responsibility Patterm) ,又叫责任链模式,为请求创建了一个接收者对象的链,这种模式对请求的发送者和接收者进行解耦。

  • 职责链模式通常每个接收者都包含对另一个接收者的引用。如果这个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推。



1、如果金额 0< X <=5000,由教学主任审批;
2、如果金额 5000< X <=10000,由院长审批;
3、如果金额 10000< X <=30000,由副校长审批;
4、如果金额 X >30000 以上,由校长审批。


创建请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class PurchaseRequest {

private int type = 0;//请求类型
private float price = 0.0f;//金额
private int id = 0;

public PurchaseRequest(int type, float price, int id) {
this.type = type;
this.price = price;
this.id = id;
}

public int getType() {
return type;
}

public float getPrice() {
return price;
}

public int getId() {
return id;
}
}

处理者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public abstract class Approver {

Approver approver; //下一个处理者
String name; //名字

public Approver(String name) {
this.name = name;
}

//下一个处理者
public void setApprover(Approver approver) {
this.approver = approver;
}

//处理审批请求的方法,得到一个请求,处理是子类完成的
public abstract void processRequest(PurchaseRequest purchaseRequest);
}

主任处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class DepartmentApprover extends Approver {

public DepartmentApprover(String name) {
super(name);
}

@Override
public void processRequest(PurchaseRequest purchaseRequest) {
// TODO Auto-generated method stub
if (purchaseRequest.getPrice() <= 5000) {
System.out.println("请求编号 id= " + purchaseRequest.getId() + "被" + this.name + "处理!");
} else {
approver.processRequest(purchaseRequest);
}
}
}

院长处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class CollegeApprover extends Approver {

public CollegeApprover(String name) {
super(name);
}

@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if (purchaseRequest.getPrice() > 5000 && purchaseRequest.getPrice() <= 10000) {
System.out.println("请求编号 id= " + purchaseRequest.getId() + "被" + this.name + "处理!");
} else {
approver.processRequest(purchaseRequest);
}
}
}

副校长处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class ViceSchoolMasterApprover extends Approver {

public ViceSchoolMasterApprover(String name) {
super(name);
}

@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if (purchaseRequest.getPrice() > 10000 && purchaseRequest.getPrice() <= 30000) {
System.out.println("请求编号 id= " + purchaseRequest.getId() + "被" + this.name + "处理!");
} else {
approver.processRequest(purchaseRequest);
}
}
}

校长处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class SchoolMasterApprover extends Approver {

public SchoolMasterApprover(String name) {
super(name);
}

@Override
public void processRequest(PurchaseRequest purchaseRequest) {
if (purchaseRequest.getPrice() > 30000) {
System.out.println("请求编号 id= " + purchaseRequest.getId() + "被" + this.name + "处理!");
} else {
approver.processRequest(purchaseRequest);
}
}
}

客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Client {

public static void main(String[] args) {

//创建一个请求
PurchaseRequest purchaseRequest = new PurchaseRequest(1, 5100, 1);

//创建相关审批人
DepartmentApprover departmentApprover = new DepartmentApprover("主任");
CollegeApprover collegeApprover = new CollegeApprover("院长");
ViceSchoolMasterApprover viceSchoolMasterApprover = new ViceSchoolMasterApprover("副校长");
SchoolMasterApprover schoolMasterApprover = new SchoolMasterApprover("校长");

//需要将各个审批级别的下一个设置进去(形成环形)否则会 空指针
departmentApprover.setApprover(collegeApprover);
collegeApprover.setApprover(viceSchoolMasterApprover);
viceSchoolMasterApprover.setApprover(schoolMasterApprover);
schoolMasterApprover.setApprover(departmentApprover);

departmentApprover.processRequest(purchaseRequest);
viceSchoolMasterApprover.processRequest(purchaseRequest);
}
}

责任链模式的注意事项和细节

  • 将请求和处理分开,实现解耦,提高系统的灵活性;
  • 简化了对象,使对象不需要知道链的结构;
  • 性能会受到影响,特别是在链比较长的时候,因此需控制链中最大节点数量,一般通过在 Handler 中设置一个最大节点数量,在 setNext() 方法中判断是否已经超过阀值,超过则不允许该链建立,避免出现超长链无意识地破坏系统性能;
  • 调试不方便。采用了类似递归的方式,调试时逻辑可能比较复杂;
  • 最佳应用场景:有多个对象可以处理同一个请求时,比如:多级请求、请假/加薪等审批流程、JavaWeb中Tomcat对Encoding的处理、拦截器。

职责链模式在 SpringMVC 应用场景

SpringMVC 中 HandlerExecutionChain 就使用到了职责链模式。

  • SpringMVC 请求的流程中,执行了拦截器相关方法 interceptor preHandler 等;
  • 在处理 SpringMvc 请求时,使用到责任链模式还使用到适配器模式;
  • HandlerExecutionChain 主要负责的是请求拦截器的执行和请求处理,但是他本身不处理请求,只是将请求分配给链上注册处理器执行,这是职责链实现方式,减少职责链本身与处理逻辑之间的耦合,规范了处理流程;
  • HandlerExecutionChain 维护了 HandlerInterceptor 集合,可以向其中注册相应拦截器。

感谢阅读


If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !