责任链模式

责任链:每个对象有对其下家的引用而形成的一条链
当前对象自己条件不符合或处理不了,
交由下家对象处理,一直传递下去直到处理完请求
下家对象是什么鬼:就是new出自家的时候化设置的下家对象

缘起

客服系统中消息回复超时提示告警,超时时间以不同方式提示具体的业务对象执行

  • 时间边界– 5分钟/10分钟/30分钟内客服没有回复用户信息则提示
  • 提示方式– 短信/微信/邮件/钉钉等
  • 提示对象有– 客服/老板/系统清除对象

直接上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* 告警责任链
*
* @author hubo
* @since 2017-06-29 18:53
**/
public abstract class AlarmHandler {
protected AlarmHandler nextAlarmHandler;
protected AlarmHandler getNextAlarmHandler() {
return nextAlarmHandler;
}
/**
* 告警
*
* @param ruleTime 规则时间
*/
protected abstract void alarm(final long ruleTime, final AlarmEvent alarmInfo);
}
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
/**
* 客服链对象
* 5分钟时间客服不回复访客消息则提示客服
*
* @author hubo
* @since 2017-06-29 18:57
**/
public class AlarmToCustomer extends AlarmHandler {
public AlarmToCustomer(Builder builder) {
this.nextAlarmHandler = builder.nextHandler;
}
/**
* 告警业务操作
*
* @param ruleTime 规则时间
* @param alarmInfo
*/
public void alarm(final long ruleTime, final AlarmEvent alarmInfo) {
if (ruleTime >= 5) {
System.out.println("超过5分钟,提示客服");
} else {
if (getNextAlarmHandler() != null)
getNextAlarmHandler().alarm(ruleTime, alarmInfo);
}
}
public static class Builder {
private AlarmHandler nextHandler;
public Builder() {
}
public Builder(AlarmHandler nextHandler) {
this.nextHandler = nextHandler;
}
public Builder nextHandler(AlarmHandler nextHandler) {
this.nextHandler = nextHandler;
return this;
}
public AlarmToCustomer build() {
return new AlarmToCustomer(this);
}
}
}
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
/**
* Boss告警链
* 15分钟时间客服不回复访客消息通知Boss
*
* @author hubo
* @since 2017-06-29 18:57
**/
public class AlarmToBoss extends AlarmHandler {
public AlarmToBoss(Builder builder) {
this.nextAlarmHandler = builder.nextHandler;
}
/**
* 告警业务操作
*
* @param ruleTime 规则时间
* @param alarmInfo
*/
public void alarm(final long ruleTime, final AlarmEvent alarmInfo) {
if (ruleTime >= 15) {
System.out.println("超过15分钟,告知Boss,客服在打酱油");
} else {
if (getNextAlarmHandler() != null)
getNextAlarmHandler().alarm(ruleTime, alarmInfo);
}
}
public static class Builder {
private AlarmHandler nextHandler;
public Builder() {
}
public Builder nextHandler(AlarmHandler nextHandler) {
this.nextHandler = nextHandler;
return this;
}
public AlarmToBoss build() {
return new AlarmToBoss(this);
}
}
}
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分钟时间客服不回复访客清除任务
*
* @author hubo
* @since 2017-06-29 18:57
**/
public class AlarmToDestroy extends AlarmHandler {
public AlarmToDestroy() {
}
/**
* 告警业务操作
*
* @param ruleTime 规则时间
* @param alarmInfo
*/
public void alarm(final long ruleTime, final AlarmEvent alarmInfo) {
if (ruleTime >= 30) {
System.out.println("超过30分钟,不再提示了");
} else {
if (getNextAlarmHandler() != null)
getNextAlarmHandler().alarm(ruleTime, alarmInfo);
}
}
}

应用

1
2
3
4
5
6
7
8
//组合责任链
AlarmToDestroy destroy = new AlarmToDestroy();
AlarmToBoss boss = new AlarmToBoss.Builder().nextHandler(destroy).build();
AlarmToCustomer customer = new AlarmToCustomer.Builder().nextHandler(boss).build();
customer.alarm(4, new AlarmInfo());
customer.alarm(20, new AlarmInfo());
customer.alarm(35, new AlarmInfo());