工厂模式:通过一个工厂类根据不同的条件获取不同的实现对象
@Component
public class FlowHandleFactory {
private final Map<Integer, FlowHandleService> handleServiceMap = new HashMap<>();
@Autowired
public FlowHandleFactory(@Qualifier("scrapAuditHandleServiceImpl") ScrapAuditHandleServiceImpl scrapAuditHandleServiceImpl,
@Qualifier("flowHandleServiceImpl") FlowHandleServiceImpl flowHandleServiceImpl) {
handleServiceMap.put(1, scrapAuditHandleServiceImpl);
handleServiceMap.put(2, flowHandleServiceImpl);
}
public FlowHandleService getFlowHandleService(Integer type) {
return handleServiceMap.get(type);
}
}
或者通过获取上下文的Bean实现
@Component
public class FlowHandleFactory implements ApplicationContextAware {
private final Map<Integer, FlowHandleService> handleServiceMap = new HashMap<>();
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(@NotNull ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@PostConstruct
public void init(){
handleServiceMap.put(FlowModuleEnum.DEVICE_SCRAP.getType(),applicationContext.getBean(ScrapAuditHandleServiceImpl.class));
}
public FlowHandleService getFlowHandleService(Integer type) {
return handleServiceMap.get(type);
}
}
使用的时候
FlowHandleService flowHandleService = flowHandleFactory.getFlowHandleService(FlowModuleEnum.DEVICE_SCRAP.getType());
return flowHandleService.handleService(instance,nodeTaskDTO);
Java中多实现的工厂模式

被折叠的 条评论
为什么被折叠?



