一个接口多个实现

Java中多实现的工厂模式

工厂模式:通过一个工厂类根据不同的条件获取不同的实现对象

@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);
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值