责任链设计模式
# 为什么使用责任链设计模式?
# 责任容器
public class AuthService {
private static Map<String, Date> authMap = new ConcurrentHashMap<String, Date>();
public static Date queryAuthInfo(String uId, String orderId) {
return authMap.get(uId.concat(orderId));
}
public static void auth(String uId, String orderId) {
authMap.put(uId.concat(orderId), new Date());
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 返回信息
@Data
public class AuthInfo {
private String code;
private String info = "";
public AuthInfo(String code, String ...infos) {
this.code = code;
for (String str:infos){
this.info = this.info.concat(str);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 链路抽象类
通过链表,将责任节点链接起来,抽象一个doAuth类;
public abstract class AuthLink {
protected SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 时间格式化
protected String levelUserId;// 级别人员ID
protected String levelUserName;// 级别人员姓名
private AuthLink next;// 责任链
public AuthLink(String levelUserId, String levelUserName) {
this.levelUserId = levelUserId;
this.levelUserName = levelUserName;
}
public AuthLink next() {
return next;
}
public AuthLink appendNext(AuthLink next) {
this.next = next;
return this;
}
public abstract AuthInfo doAuth(String uId, String orderId, Date authDate);
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 链路实现类
public class L1 extends AuthLink {
public L1(String levelUserId, String levelUserName) {
super(levelUserId, levelUserName);
}
@Override
public AuthInfo doAuth(String uId, String orderId, Date authDate) {
Date date = AuthService.queryAuthInfo(levelUserId,orderId);
if(null == date){
return new AuthInfo("单号:",orderId,"状态:待一级审批负责人",levelUserName);
}
AuthLink next = super.next();
if(null == next){
return new AuthInfo("单号:",orderId,"状态:一级审批完成负责人",levelUserName,"时间:",f.format(date));
}
return next.doAuth(uId,orderId,authDate);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class L2 extends AuthLink {
private Date beginDate = f.parse("2023-12-01 00:00:00");
private Date endDate = f.parse("2023-12-05 23:59:59");
public L2(String levelUserId, String levelUserName) throws ParseException {
super(levelUserId, levelUserName);
}
@Override
public AuthInfo doAuth(String uId, String orderId, Date authDate) {
Date date = AuthService.queryAuthInfo(levelUserId,orderId);
if(null == date){
return new AuthInfo("单号:",orderId,"状态:待二级审批负责人",levelUserName);
}
AuthLink next = super.next();
if(null == next){
return new AuthInfo("单号:",orderId,"状态:二级审批完成负责人",levelUserName,"时间:",f.format(date));
}
if(authDate.before(beginDate) || authDate.after(endDate)){
return new AuthInfo("单号:",orderId,"状态:二级审批完成负责人",levelUserName,"时间:",f.format(date));
}
return next.doAuth(uId,orderId,authDate);
}
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 测试验证
public class test {
public static void main(String[] args) throws ParseException {
AuthLink authLink = new L2("1002","主任")
.appendNext(new L1("1001","锐总"));
AuthService.auth("1002","10001");
log.println("二级审批,主任");
log.println(JSON.toJSONString(authLink.doAuth("rui","10001",new Date())));
AuthService.auth("1001","10001");
log.println("一级审批,锐总");
log.println(JSON.toJSONString(authLink.doAuth("rui","10001",new Date())));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14