重构实例之-借助泛型和函数式接口

在日常开发过程中,经常发现同样的方法结构出现多次,差异较小,可以借助泛型等手段抽象出公用方法。

示例

原逻辑
// 变更出入参
  this.mergeOrDelInputParam(inputJson);
  this.mergeOrDelQaOutputParam(outputJson);

 //入参
private void mergeOrDelInputParam(String inputJson) {
        if (StringUtils.isEmpty(inputJson)) {
            log.info("入参为空");
            return;
        }
        JSONArray inputJsonArr = JSONArray.parseArray(inputJson);
        List<QaInputParam> qaInputParams = inputJsonArr.toJavaList(QaInputParam.class);
        if (CollUtil.isEmpty(qaInputParams)) {
            log.info("入参解析为空");
            return;
        }
        // 增
        List<QaInputParam> inputParams4Add = this.getQaParamsByTypes(qaInputParams, QaParamTypeEnum.ADD);
        if (CollectionUtils.isNotEmpty(inputParams4Add)) {
            log.info("入参新增");
            this.qaInputParamService.insertBatch(inputParams4Add);
        }
        // 改
        List<QaInputParam> inputParams4Edit = this.getQaParamsByTypes(qaInputParams, QaParamTypeEnum.EDIT);
        if (CollectionUtils.isNotEmpty(inputParams4Edit)) {
            log.info("入参新增");
            this.qaInputParamService.updateBatchById(inputParams4Edit);
        }

        // 删除
        List<QaInputParam> inputParams4Del = this.getQaParamsByTypes(qaInputParams, QaParamTypeEnum.DELETE);
        if (CollectionUtils.isNotEmpty(inputParams4Del)) {
            log.info("入参删除");
            this.qaInputParamService.deleteBatchIds(inputParams4Del);
        }

    }
//出参
    private void mergeOrDelQaOutputParam(String outputJson) {
        if (StringUtils.isEmpty(outputJson)) {
            log.info("出参为空");
            return;
        }
        JSONArray outputJsonArr = JSONArray.parseArray(outputJson);
        List<QaOutputParam> qaOutputParams = outputJsonArr.toJavaList(QaOutputParam.class);
        if (CollUtil.isEmpty(qaOutputParams)) {
            log.info("出参解析为空");
            return;
        }
        // 增
        List<QaOutputParam> outputParams4Add = this.getQaParamsByTypes(qaOutputParams, QaParamTypeEnum.ADD);
        if (CollectionUtils.isNotEmpty(outputParams4Add)) {
            log.info("出参新增");
            this.qaOutputParamService.insertBatch(outputParams4Add);
        }
        // 改
        List<QaOutputParam> outputParams4Edit = this.getQaParamsByTypes(qaOutputParams, QaParamTypeEnum.EDIT);
        if (CollectionUtils.isNotEmpty(outputParams4Edit)) {
            log.info("出参修改");
            this.qaOutputParamService.updateBatchById(outputParams4Edit);
        }

        // 删除
        List<QaOutputParam> outputParams4Del =
            this.getQaParamsByTypes(qaOutputParams, QaParamTypeEnum.DELETE);
        if (CollectionUtils.isNotEmpty(outputParams4Del)) {
            log.info("出参删除");
            this.qaOutputParamService.deleteBatchIds(outputParams4Del);
        }

    }

//获取要修改的数据
    private <T extends AbstractCommonParam> List<T> getQaParamsByTypes(List<T> qaParams,
        QaParamTypeEnum... types) {
        return qaParams.stream().filter(qaOutputParam -> StringUtils.isNotBlank(qaOutputParam.getType())
            && ArrayUtils.contains(types, QaParamTypeEnum.getByCode(qaOutputParam.getType())))
            .collect(Collectors.toList());
    }

重构探索1
 // 调用
 this.persistenceParam(inputJson, QaInputParam.class, this.qaInputParamService);
 this.persistenceParam(outputJson, QaOutputParam.class, this.qaOutputParamService);
 //封账
 private <T extends AbstractCommonParam> void persistenceParam(String qaParamJson, Class<T> clazz,
        IService<T> iService) {
        if (StringUtils.isEmpty(qaParamJson)) {
            log.info("参数为空");
            return;
        }
        List<T> qaParams = JSONArray.parseArray(qaParamJson).toJavaList(clazz);
        if (CollUtil.isEmpty(qaParams)) {
            log.info("参数解析为空");
            return;
        }
        // 增
        List<T> outputParams4Add = this.getQaParamsByTypes(qaParams, QaParamTypeEnum.ADD);
        if (CollectionUtils.isNotEmpty(outputParams4Add)) {
            log.info("出参新增");
           iService.insertBatch(outputParams4Add);
        }
        // 改
        List<T> outputParams4Edit = this.getQaParamsByTypes(qaParams, QaParamTypeEnum.EDIT);
        if (CollectionUtils.isNotEmpty(outputParams4Edit)) {
            log.info("出参修改");
            iService.updateBatchById(outputParams4Edit);
        }

        // 删除
        List<T> outputParams4Del =
            this.getQaParamsByTypes(qaParams, QaParamTypeEnum.DELETE);
        if (CollectionUtils.isNotEmpty(outputParams4Del)) {
            log.info("出参删除");
            iService.deleteBatchIds(outputParams4Del);
        }
    }
//获取要修改的数据
    private <T extends AbstractCommonParam> List<T> getQaParamsByTypes(List<T> qaParams,
        QaParamTypeEnum... types) {
        return qaParams.stream().filter(qaOutputParam -> StringUtils.isNotBlank(qaOutputParam.getType())
            && ArrayUtils.contains(types, QaParamTypeEnum.getByCode(qaOutputParam.getType())))
            .collect(Collectors.toList());
    }
重构探索2
//调用
this.persistenceParam(inputJson, QaInputParam.class, this.qaInputParamService);
this.persistenceParam(outputJson, QaOutputParam.class, this.qaOutputParamService);
//调用
private <T extends AbstractCommonParam> void persistenceParam(String qaParamJson, Class<T> clazz,
        IService<T> iService) {
        if (StringUtils.isEmpty(qaParamJson)) {
            log.info("参数为空");
            return;
        }
        List<T> qaParams = JSONArray.parseArray(qaParamJson).toJavaList(clazz);
        if (CollUtil.isEmpty(qaParams)) {
            log.info("参数解析为空");
            return;
        }
        // 变更
        this.doPersistenceParam(qaParams, QaParamTypeEnum.ADD, iService::insertBatch);
        this.doPersistenceParam(qaParams, QaParamTypeEnum.EDIT, iService::updateBatchById);
        this.doPersistenceParam(qaParams, QaParamTypeEnum.DELETE,
            (List<T> list) -> iService.deleteBatchIds(list.stream().map(T::getId).collect(Collectors.toList())));
    }
//持久化
private <T extends AbstractCommonParam> void doPersistenceParam(List<T> qaParams, QaParamTypeEnum qaParamTypeEnum,
        Function<List<T>, Boolean> function) {
        List<T> qaParams4Handle = this.getQaParamsByTypes(qaParams, qaParamTypeEnum);
        if (CollectionUtils.isNotEmpty(qaParams4Handle)) {
            log.info("参数持久化,类型={}", qaParamTypeEnum);
            function.apply(qaParams4Handle);
        }
    }
//获取要修改的数据
    private <T extends AbstractCommonParam> List<T> getQaParamsByTypes(List<T> qaParams,
        QaParamTypeEnum... types) {
        return qaParams.stream().filter(qaOutputParam -> StringUtils.isNotBlank(qaOutputParam.getType())
            && ArrayUtils.contains(types, QaParamTypeEnum.getByCode(qaOutputParam.getType())))
            .collect(Collectors.toList());
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白萝卜弟弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值