Builder模式

直接上代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* 告警责任链
*
* @author hubo
* @since 2017-06-29 18:53
**/
public abstract class AlarmHandler {
protected AlarmHandler nextAlarmHandler;
protected AlarmHandler getNextAlarmHandler() {
return nextAlarmHandler;
}
}
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
/**
* 客服链对象
* 5分钟时间客服不回复访客消息则提示客服
*
* @author hubo
* @since 2017-06-29 18:57
**/
public class AlarmToCustomer extends AlarmHandler {
public AlarmToCustomer(Builder builder) {
this.nextAlarmHandler = builder.nextHandler;
}
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
/**
* Boss告警链
* 15分钟时间客服不回复访客消息通知Boss
*
* @author hubo
* @since 2017-06-29 18:57
**/
public class AlarmToBoss extends AlarmHandler {
public AlarmToBoss(Builder builder) {
this.nextAlarmHandler = builder.nextHandler;
}
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
/**
* 销毁任务对象链
* 超过30分钟时间客服不回复访客清除任务
*
* @author hubo
* @since 2017-06-29 18:57
**/
public class AlarmToDestroy extends AlarmHandler {
public AlarmToDestroy() {
}
}

应用

1
2
3
4
//组合应用
AlarmToDestroy destroy = new AlarmToDestroy();
AlarmToBoss boss = new AlarmToBoss.Builder().nextHandler(destroy).build();
AlarmToCustomer customer = new AlarmToCustomer.Builder().nextHandler(boss).build();