zoukankan      html  css  js  c++  java
  • 模板模式下泛型的简单使用

    直接放代码。。。。

    接口定义(为限制请求和响应的参数类型,请求和响应参数设置为泛型)

     1 public interface IFundFlowService<Req extends BaseRequest,Resp extends BaseResponse> {
     2 
     3     /**
     4      * 资金操作
     5      *
     6      * @param request 资金操作请求对象
     7      */
     8     Resp execute(Req request);
     9 
    10     /**
    11      * 查询
    12      * @param requestId
    13      * @return
    14      */
    15     Resp query(String requestId);
    16 
    17 }
    View Code

    模板类(实现了核心方法,子类了继承该模板类并实现抽象方法):

     1 @Service
     2 public abstract class FundFlowTemplate<Req extends BaseRequest,Resp extends BaseResponse>
     3         implements IFundFlowService<Req,Resp> {
     4     private static Logger logger = LoggerFactory.getLogger(FundFlowTemplate.class);
     5 
     6     /**
     7      * 事务模板
     8      */
     9     @Autowired
    10     private TransactionTemplate txTemplate;
    11 
    12 
    13     /**
    14      * 资金流转
    15      *
    16      * @param request
    17      */
    18     @Override
    19     public Resp execute(Req request) {
    20         //构建执行上下文
    21         FundContext fundContext = this.buildContext(request);
    22 
    23         try {
    24             //01、业务数据校验
    25             this.busiValid(fundContext);
    26 
    27             //02、请求数据落地
    28             this.save(fundContext);
    29 
    30             //03、加载账户(&账户不存在则开户)
    31             this.loadAndOpenAcct(fundContext);
    32 
    33             //04、资金操作
    34             this.fundOperate(fundContext);
    35 
    36         } catch (Exception e) {
    37             //TODO 统一异常处理
    38         } finally {
    39             // 更新请求状态
    40             this.update(fundContext);
    41         }
    42 
    43         //TODO 构建响应结果
    44         return null;
    45     }
    46 
    47 
    48 
    49     /**
    50      * 更新请求状态
    51      * --业务调用方必须实现
    52      *
    53      * @param fundContext
    54      */
    55     protected abstract void update(FundContext fundContext);
    56 
    57 
    58 }
    View Code

    最关键的请注意:实现的接口中未传入泛型实参,模板类需要将泛型的声明一起加入类中

    实现类1:

     1 @Service
     2 public class FundFrozenServiceImpl extends FundFlowTemplate<ACCTFrozenReq, ACCTFrozenResp>{
     3     private final RequestTypeEnum fundType = RequestTypeEnum.FROZEN;
     4 
     5 
     6     @Override
     7     public ACCTFrozenResp query(String requestId) {
     8         return null;
     9     }
    10 
    11 }
    View Code
  • 相关阅读:
    org.apache.maven.archiver.MavenArchiver.getManifest
    网易云信发送短信验证码
    background-attachment:fixed;
    background-size属性100% cover contain
    width:100% width:auto 区别
    背景图全屏显示
    多行文字的垂直居中
    路径问题../绝对路径/
    用position子绝父相和css3动画,当鼠标一上去元素从底部慢慢向上
    前置后置运算符重载
  • 原文地址:https://www.cnblogs.com/huahua035/p/10225025.html
Copyright © 2011-2022 走看看