Builder模式 发表于 2017-07-16 12:10:06 | 分类于 设计模式 | 阅读次数 字数统计 246 | 阅读时长 1 直接上代码123456789101112131415/** * 告警责任链 * * @author hubo * @since 2017-06-29 18:53 **/public abstract class AlarmHandler { protected AlarmHandler nextAlarmHandler; protected AlarmHandler getNextAlarmHandler() { return nextAlarmHandler; }} 1234567891011121314151617181920212223242526272829303132333435/** * 客服链对象 * 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); } }} 12345678910111213141516171819202122232425262728293031/** * 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); } }} 1234567891011121314/** * 销毁任务对象链 * 超过30分钟时间客服不回复访客清除任务 * * @author hubo * @since 2017-06-29 18:57 **/public class AlarmToDestroy extends AlarmHandler { public AlarmToDestroy() { }} 应用1234//组合应用AlarmToDestroy destroy = new AlarmToDestroy();AlarmToBoss boss = new AlarmToBoss.Builder().nextHandler(destroy).build();AlarmToCustomer customer = new AlarmToCustomer.Builder().nextHandler(boss).build();