0%

设计模式-责任链模式

Tomcat的过滤器也使用到了责任链ApplicationFilterChain,具体的还没有读,先写完这个模式,比模板模式感觉有点复杂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
public abstract class Handler {
protected String name; // 处理者姓名

@Setter
protected Handler nextHandler; // 下一个处理者

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

public abstract boolean process(LeaveRequest leaveRequest); // 处理请假
}

public class Director extends Handler {

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

@Override
public boolean process(LeaveRequest leaveRequest) {
// 随机数大于3则为批准,否则不批准
boolean result = (new Random().nextInt(10)) > 3;
String log = "主管<%s> 审批 <%s> 的请假申请,请假天数: <%d> ,审批结果:<%s> ";
System.out.println(String.format(log, this.name, leaveRequest.getName(), leaveRequest.getNumOfDays(), result == true ? "批准" : "不批准"));

if (!result) { // 不批准
return false;
} else if (leaveRequest.getNumOfDays() < 3) { // 批准且天数小于3,返回true
return true;
}
return nextHandler.process(leaveRequest); // 批准且天数大于等于3,提交给下一个处理者处理

}
}

public class Manager extends Handler {

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

@Override
public boolean process(LeaveRequest leaveRequest) {
boolean result = (new Random().nextInt(10)) > 3; // 随机数大于3则为批准,否则不批准
String log = "经理<%s> 审批 <%s> 的请假申请,请假天数: <%d> ,审批结果:<%s> ";
System.out.println(String.format(log, this.name, leaveRequest.getName(), leaveRequest.getNumOfDays(), result == true ? "批准" : "不批准"));

if (!result) { // 不批准
return false;
} else if (leaveRequest.getNumOfDays() < 7) { // 批准且天数小于7
return true;
}
return nextHandler.process(leaveRequest); // 批准且天数大于等于7,提交给下一个处理者处理
}
}

public class TopManager extends Handler {

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

@Override
public boolean process(LeaveRequest leaveRequest) {
boolean result = (new Random().nextInt(10)) > 3; // 随机数大于3则为批准,否则不批准
String log = "总经理<%s> 审批 <%s> 的请假申请,请假天数: <%d> ,审批结果:<%s> ";
System.out.println(String.format(log, this.name, leaveRequest.getName(), leaveRequest.getNumOfDays(), result == true ? "批准" : "不批准"));

if (!result) { // 总经理不批准
return false;
}
return true; // 总经理最后批准
}
}


@Data
@AllArgsConstructor
public class LeaveRequest {

private String name; // 请假人姓名
private int numOfDays; // 请假天数

}

public class Test {

public static void main(String[] args) {
// 创建审批人
Handler zhangsan = new Director("张三");
Handler lisi = new Manager("李四");
Handler wangwu = new TopManager("王五");

// 创建责任链
zhangsan.setNextHandler(lisi);
lisi.setNextHandler(wangwu);

// 发起请假申请第一次
boolean result1 = zhangsan.process(new LeaveRequest("小旋锋", 1));
System.out.println("最终结果:" + result1 + "\n");

// 发起请假申请第二次
boolean result2 = zhangsan.process(new LeaveRequest("小旋锋", 4));
System.out.println("最终结果:" + result2 + "\n");

// 发起请假申请第三次
boolean result3 = zhangsan.process(new LeaveRequest("小旋锋", 8));
System.out.println("最终结果:" + result3 + "\n");
}
}

参考的是:https://juejin.im/post/6844903702260629512#heading-11
感觉举的例子的业务显示还不是特别的好理解,等之后把SpringMVC的源码学文案了,再举个例子