当前位置: 代码迷 >> 综合 >> java 回调写法
  详细解决方案

java 回调写法

热度:71   发布时间:2024-02-06 22:50:44.0

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

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);