不同请假天数需要不同级别人员审核,所以将不同处理类定义成一个责任链
1、责任链执行抽象类
编写过滤责任链执行抽象类AbstractExecuteFilter.java
public abstract class AbstractExecuteFilter {
public static final String SLOT_KEY = "LeaveRequest";
protected AbstractExecuteFilter nextFilter;
public static ThreadLocal<Map<String, Object>> localMap = new ThreadLocal<>();
public void addNextFilter(AbstractExecuteFilter filter) {
this.nextFilter = filter;
}
public abstract boolean execute();
protected int MIN = 1;
protected int MIDDLE = 3;
protected int MAX = 30;
}
编写参数传递类(请假类):
@Builder
public class LeaveRequest {
private int leaveDays;
private String name;
}
2、设置上下文环境获取bean
根据不同的filters名动态获取对象,ExecuteFilterLoader实现ApplicationContextAware接口,
@Slf4j
@Component
public class ExecuteFilterLoader implements ApplicationContextAware {
public static ApplicationContext applicationContext;
public static AbstractExecuteFilter loadExecuteFilter(List<String> filters) {
if (CollectionUtils.isEmpty(filters)) {
throw BusinessException.asBusinessException(BusinessErrorCode.ILLEGAL_ARG_ERROR);
}
AbstractExecuteFilter filter = null;
AbstractExecuteFilter next = null;
for (String filterName : filters) {
AbstractExecuteFilter bean = (AbstractExecuteFilter) applicationContext.getBean(filterName);
if (null == next) {
filter = bean;
} else {
next.addNextFilter(bean);
}
next = bean;
}
return filter;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ExecuteFilterLoader.applicationContext = applicationContext;
}
}
3、不同处理类扩展 AbstractLeaveHandler
创建三种类型的审批处理,都继承了 AbstractLeaveHandler抽象类:
@Slf4j
@Component("directLeaderLeaveHandler")
public class DirectLeaderLeaveHandler extends AbstractLeaveHandler {
@Override
public void execute() {
int leaveDays = (int) localMap.get().get("leaveDays");
if(leaveDays <= this.MIN){
System.out.println("直接主管已经处理;流程结束。");
return;
}
if(null != this.nextFilter){
this.nextFilter.execute();
}else{
System.out.println("审批拒绝!");
}
}
}
@Slf4j
@Component("deptManagerLeaveHandler")
public class DeptManagerLeaveHandler extends AbstractLeaveHandler {
@Override
public void execute() {
int leaveDays = (int) localMap.get().get("leaveDays");
if(leaveDays >this.MIN && leaveDays <= this.MIDDLE){
System.out.println("部门经理已经处理;流程结束。");
return;
}
if(null != this.nextFilter){
this.nextFilter.execute();
}else{
System.out.println("审批拒绝!");
}
}
}
@Slf4j
@Component("gManagerLeaveHandler")
public class GManagerLeaveHandler extends AbstractLeaveHandler {
@Override
public void execute() {
int leaveDays = (int) localMap.get().get("leaveDays");
if(leaveDays > this.MIDDLE && leaveDays <= this.MAX){
System.out.println("总经理已经处理;流程结束。");
return;
}
if(null != this.nextFilter){
this.nextFilter.execute();
}else{
System.out.println("审批拒绝!");
}
}
}
4、调用
(新员工请假20天谁审批):
String filterConfig="directLeaderLeaveHandler,deptManagerLeaveHandler,gManagerLeaveHandler";
LeaveRequest request = LeaveRequest.builder().leaveDays(20).name("新员工").build();
Map<String, Object> localMap = new HashMap<>();
localMap.put(AbstractExecuteFilter.SLOT_KEY,request);
AbstractExecuteFilter.localMap.set(localMap);
ExecuteFilterLoader.loadExecuteFilter(Arrays.asList(filterConfig.split(","))).execute();
运行结果:总经理已经处理;流程结束。
小结:调用了审批链不同环节的处理,应用责任链模式进行解耦,增强给对象指派职责的灵活性。
|