责任链模式
以(StatisticalFactorImpl)实现类为例(dateType类型过多)
1、先抽象出一个接口
IStatisticalFactorDateTypeService
2、在IStatisticalFactorDateTypeService接口中新增方法
Boolean canDo(String dateType);
3、增加IStatisticalFactorDateTypeService的实现类,然后实现canDo方法
@Override
public Boolean canDo(String dateType) {
return TimeCategoryEnum.DAY.getValue().equals(dateType);
}
4、引入到涉稳因素(StatisticalFactorImpl)中
protected final List<IStatisticalFactorDateTypeService> statisticalFactorDateTypeServices;
public StatisticalFactorImpl(List<IStatisticalFactorDateTypeService> statisticalFactorDateTypeServices) {
this.statisticalFactorDateTypeServices = statisticalFactorDateTypeServices;
}
5、根据(dateType)判断走IStatisticalFactorDateTypeService的哪一个实现类
protected IStatisticalFactorDateTypeService getDateTypeService(String dateType) {
return statisticalFactorDateTypeServices.stream().filter(r -> r.canDo(dateType)).findFirst().get();
}