对于一个抽象类有多个实现类怎么获取

本文探讨了在与资方合作中,通过抽象类、实现类容器、具体实现及灵活的接口设计,如何利用工厂模式管理多种贷款回调和补偿处理类。介绍了三种解决方案,包括抽象接口、容器管理、具体实现,并展示了如何根据需求选择和组合这些技术来实现高效协作。
部署运行你感兴趣的模型镜像

对接资方时,肯定会有多个实现类很正常

我们提供三种解决方法:

  • 1.1抽象类:
// 辅助类
public interface BeanSupport {
    boolean support(String var1);
}
// 抽象业务类
public interface LoanCallBack extends BeanSupport {
    /**
     * 回调通知
     *
     * @param loanApplyRecord the loan apply record
     * @return the call back resp
     */
    CallBackResp callBackNotify(LoanApplyRecord loanApplyRecord);
}



  • 1.2实现类的容器
@Component
public class SupportFactoryUtils implements ApplicationContextAware {
    public static ApplicationContext applicationContext;

    public SupportFactoryUtils() {
    }

    public static <T extends BeanSupport> T get(String supportType, Class<T> cls) {
        Map<String, T> beanMap = applicationContext.getBeansOfType(cls);
        Iterator i$ = beanMap.values().iterator();

        BeanSupport t;
        do {
            if (!i$.hasNext()) {
                return null;
            }

            t = (BeanSupport)i$.next();
        } while(!t.support(supportType));

        return t;
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SupportFactoryUtils.applicationContext = applicationContext;
    }
}

  • 1.3实现类
@Component
@Slf4j
public class FqlLoanCallBack implements LoanCallBack {

 


    @Override
    public CallBackResp callBackNotify(LoanApplyRecord loanApplyRecord) {
        
        return cashOrderResult;
    }

    @Override
    public boolean support(String type) {
        return ChannelFlowNames.CHANNEL_FQL.equals(type)|| ChannelCodes.FQL.equals(type);
    }
  

}

  • 1.4 具体使用
	LoanCallBack callBack = SupportFactoryUtils.get(loanApplyRecord.getChannelCode(), LoanCallBack.class);

第二种方案

  • 2.1抽象类
public interface IFundCompensatory {

    /**
     * 各资方处理代偿逻辑
     *
     * @param fundCompensatoryConfig
     */
//    @RpcMethod(async = true)
    void compensatory(FundCompensatoryReq fundCompensatoryReq);

    /**
     * @param fundCompensatoryConfig
     * @return
     */
    boolean support(FundCompensatoryReq fundCompensatoryReq);

    boolean singleCompensatory(FundCompensatoryReq fundCompensatoryReq);

}
  • 2.2抽象类兼容每个实现类入参出参不一致时
 
public abstract class AbstractFundCompensatoryProcessor<T> implements IFundCompensatory {

     
    @Override
    public void compensatory(FundCompensatoryReq compensatoryReq) {
        logger.info("Start to compensatory by compensatoryReq={}", JsonUtil.toJson(compensatoryReq));
           // step1 : 获取
            List<T> repaySchedules = preProcessRepaySchedule(compensatoryReq, page);
            List<CompensatoryResp> compensatoryResps = Lists.newArrayList();
            do {
                if (CollectionUtil.isEmpty(repaySchedules)) {
                    break;
                }
                logger.info("compensatory#preProcessRepaySchedule step2");
                // step2: 保存初始化记录
                saveInitCompensatoryRecord(repaySchedules, compensatoryReq, batchNo);
                logger.info("compensatory#preProcessRepaySchedule step3");
                page++;
                logger.info("compensatory#preProcessRepaySchedule page={}", page);
                repaySchedules.clear();
                repaySchedules = preProcessRepaySchedule(compensatoryReq, page);
            } while (CollectionUtil.isNotEmpty(repaySchedules));
            // step3: 根据代偿记录发起 请求
            QueryResult<CompensatoryRecord> compensatoryRecordQueryResult = queryInitCompensatoryRecord(batchNo);
          
    } 
    protected abstract BaseResp process(T repaySchedule, FundCompensatoryReq fundCompensatoryReq)
            throws AppBizException;
  
    protected abstract List<T> preProcessRepaySchedule(FundCompensatoryReq fundCompensatoryReq, int page); 
    
    protected abstract void saveInitCompensatoryRecord(List<T> repaySchedules, FundCompensatoryReq compensatoryReq, String batchNo);

   
    protected abstract QueryResult<CompensatoryRecord> queryInitCompensatoryRecord(String batchNo);

}

  • 2.3 具体的实现类
@Service
public class ThirdFundChargeBackProcessor extends AbstractFundCompensatoryProcessor<RepaySchedule> {

   

    @Override
    protected BaseResp process(ThirdRepaySchedule repaySchedule, FundCompensatoryReq fundCompensatoryReq) throws AppBizException {
        BaseResp resp = new BaseResp();
        return resp;
    }

   
  
    @Override
    public boolean support(FundCompensatoryReq fundCompensatoryReq) {
        //  
         
    }

    @Override
    public boolean singleCompensatory(FundCompensatoryReq fundCompensatoryReq) {
        return false;
    }
}
  • 2.5具体使用
@Service
public class ProcessCompensatoryImpl implements IProcessCompensatory {

   

    @Autowired
    private List<IFundCompensatory> fundCompensatories;

 

    @Override
    public void processCompensatory(Long id) {
    
 
        for (IFundCompensatory fundCompensatory : fundCompensatories) {
            if (!fundCompensatory.support(compensatoryReq)) {
                continue;
            }
            fundCompensatory.compensatory(compensatoryReq);
        }
    }

}

第三种: 如果实现类是一整套不一样的流程,那么就这样

  • 3.1定义整个流程的类
@Root
public class TxnFlowConfig {

	/**
	 * sdk调用
	 */
	@Element(name = "http-invoke", required = false)
	private HttpInvoke sdkInvoke;

	/**
	 * 批次任务
	 */
	@ElementList(name = "jobs", required = false)
	private List<Job> jobs;

	/**
	 * 交易流程定义
	 */
	@ElementList(inline = true, required = false)
	private List<TxnFlowDef> txnFlowDefs;

	/**
	 * 流程步骤定义
	 */
	@ElementList(inline = true, required = false)
	private List<TxnFlowStepDef> flowStepDefs;
}
  • 3.2将整个流程放入内存容器
@Service 
public class TxnFlowAdminService implements InitializingBean {
	private final Logger LOG = LoggerFactory.getLogger(this.getClass());
	/**
	 * 交易流程配置Map
	 */
	public final Map<String, TxnFlowConfig> txnFlowConfigMap = new ConcurrentHashMap<String, TxnFlowConfig>();
	 

	@Override
	public GateTxnFlowConfig getGateTxnFlowConfig(Long id) {
		if(id == null){
			return null;
		}
		return gateTxnFlowConfigDao.get(id);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
	}
 

}

  • 3.3使用
   TxnFlowDef txnFlowDef = txnFlowService.getTxnFlowDef(gateId, ctx);

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值