策略模式
if条件的终结者,不同的条件调用不同的处理类进行处理;
1.定义一个顶层接口,继承InitializingBean接口,保证在容器启动时将实现该接口的类实例化交给容器管理;
public interface ImportDataHandler<T> extends InitializingBean {
/**
* 处理导入数据
*
* @param bill
* @return
*/
BillDataDto handleImportData(BillDataDto bill);
void handleDatas(List<T> datas);
//处理类标记
String getBillNumber();
@Override
default void afterPropertiesSet(){
Factory.registerHandler(this);
}
//内部工厂类对实现实现类添加到hashMap中
class Factory {
private static Map<String, ImportDataHandler> handlerMap = new HashMap<>();
private static void registerHandler(ImportDataHandler handleService) {
if (handleService != null) {
handlerMap.putIfAbsent(handleService.getBillNumber(), handleService);
}
}
public static ImportDataHandler getHandler(String billNumber) {
return handlerMap.get(billNumber);
}
}
}
调用方通过String getBillNumber()方法获取标记,继而从handlerMap拿到具体对应的实现类处理对象
ImportDataHandler handler = ImportDataHandler.Factory.getHandler(pictx.getBillnum());
然后进行后续操作。

浙公网安备 33010602011771号