java 回调写法

有许多是共有的逻辑,但中间穿插着不同逻辑,怎么相对简洁的实现呢?

1.定义模板类

public class DemandRelationTemplate {

  

    /**
     * 模版方法
     * REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public BusinessReleaseBatch template(final DemandRelationDto demandRelationDto, CallBack<BusinessReleaseBatch> callBack) {
      
        //检查参数(共同逻辑)
        checkArgument(demandRelationDto);

        //回调方法(不一样的逻辑)
        BusinessReleaseBatch releaseBatch = callBack.doCallBack();

        //数据更新(共同逻辑)
        this.updateExtend(demandRelationDto, releaseBatch);

        //更新数据
        this.updateDemand(demandRelationDto);

        return releaseBatch;
    }

    /**
     * 检查参数
     */
    private void checkArgument(DemandRelationDto demandRelationDto) {
        Preconditions.checkArgument(demandRelationDto != null, "请求不能为空");

      
    }
    private void updateDemand(){DemandRelationDto demandRelationDto}


}

 

调用模板方法:

demandRelationTemplate.template(demandRelationDto, () ->
                this.submitReleaseApplication(businessIdentityChild, versionManageEntity, erp, martrixSiteEnum));

 

执行过程:1.调用template方法,执行逻辑 checkArgument(demandRelationDto)

                  2.回调执行submitReleaseApplication(businessIdentityChild, versionManageEntity, erp, martrixSiteEnum)

                  3.执行template中后续逻辑:
               this.updateExtend(demandRelationDto, releaseBatch);
              this.updateDemand(demandRelationDto);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值