一.概念
1.1 概念
责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链。这种模式给予请求的类型,对请求的发送者和接收者进行解耦。这种类型的设计模式属于行为型模式。
在这种模式中,通常每个接收者都包含对另一个接收者的引用。如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推。
1.2 结构
职责链模式主要包含以下角色:
抽象处理者(Handler)角色:定义一个处理请求的接口,包含抽象处理方法和一个后继连接。
具体处理者(Concrete Handler)角色:实现抽象处理者的处理方法,判断能否处理本次请求,如果可以处理请求则处理,否则将该请求转给它的后继者。
客户类(Client)角色:创建处理链,并向链头的具体处理者对象提交请求,它不关心处理细节和请求的传递过程。
二.场景
现场出现特殊需要处理的情况、客户电话投诉或者订单发生状态转变需要触发其他连带修改的场景,一般都是一线人员会创建工单跟进,然后工单是有一定的处理流程的:
客服专员先审核工单,能处理则自己处理,不能处理则转到客服领班;
客服领班审核工单,能处理则自己处理,不能处理则转到客服经理;
客服经理处理工单,得到工单的最终状态。
三.类图及代码实现
3.1 类图
3.2 代码实现
3.2.1 审核抽象类定义
@Data
public abstract class VerifyService {
protected String optId;
protected String optName;
protected String type;
private VerifyService next;
public VerifyService(String optId, String optName, String type) {
this.optId = optId;
this.optName = optName;
this.type = type;
}
public VerifyService getNext() {
return this.next;
}
public VerifyService setNext(VerifyService next) {
this.next = next;
return this;
}
public abstract String verify(String optId, String optName, String type);
}
3.2.2 客服专员初审
@Slf4j
public class CommissionerVerify extends VerifyService {
public CommissionerVerify(String userId, String userName, String type) {
super(userId, userName, type);
}
@Override
public String verify(String optId, String optName, String type) {
if (this.getType().equalsIgnoreCase(type)) {
log.info("客服专员id:{},名称:{},处理成功, it is so {}", optId, optName, type);
return