zoukankan      html  css  js  c++  java
  • 设计模式之模板方法

    模板方法是用得最广泛的设计模式之一,特别是在应用框架中经常用模板方法来实现控制反转(Inversion of Control, IoC)。

    比如在批处理框架中,每个批处理任务都从框架的层面上都可以分成如下三个步骤:

    1. 初始化

    2. 处理

    3. 结束清理

    在EL中,BaseEscrowLedgerApp定义了一个process方法,该方法规定了每个EscrowLedgerApp应用的总体处理流程

    1. init
    2. validateRequest
    3. processInternally
    4. commit
    5. handleResponse

    View Code
     1     public BaseEscrowLedgerResponse process(BaseEscrowLedgerRequest request){
    2 boolean successAction = false;
    3 try {
    4 //init
    5 init(request);
    6
    7 // validate request
    8 validateRequest();
    9
    10 // process request internally
    11 processInternally();
    12
    13 // commit db change
    14 commit();
    15
    16 //build response
    17 buildResponse();
    18
    19 //successAction
    20 successAction = true;
    21 } catch (Exception e) {
    22 handleException(e);
    23 } finally {
    24 // clear up
    25 // clear UOW token
    26 cleanup(successAction);
    27 }
    28 return getResponse();
    29 }

    process方法即为模板方法,而其他的几个方法除commit之外都是抽象方法。每个具体的EscrowLedgerApp应用根据自己的实际情况来实现上述抽象方法。
    commit方法由于对各个子类来说都是提交封装在EmsPaymentBo中的数据变化,所以在BaseEscrowLedgerApp中提供一个默认实现是比较合理的。代码如下。

    View Code
    1     protected void commit() throws CreateException, UpdateException {
    2 EmsPaymentBo payment = getPaymentChanges();
    3 if(payment != null){
    4 EmsPaymentAggregatorBOF.getInstance().executeBulkOperations(payment);
    5 }
    6 }


    以CreateLienApp为例,
    init

    View Code
    1     protected void init(BaseEscrowLedgerRequest request) {
    2 m_request = (CreateLienRequest) request;
    3 m_response = new CreateLienResponse();
    4 m_paymentChanges = new EmsPaymentBo();
    5 m_lienTrx = m_request.getLienTrxInfo();
    6 }

    validateRequest

    View Code
     1     protected void validateRequest() throws EmsValidationException {
    2 m_lienTrx = m_request.getLienTrxInfo();
    3 if(m_lienTrx == null){
    4 throw EmsExceptionHelper.newValidationFailedException("LienTrxInfo", "lienTrxInfo is missing");
    5 }
    6
    7 if (m_lienTrx.getPayerId() <= 0) {
    8 throw EmsExceptionHelper.newValidationFailedException("PayerId", "payerId is missing");
    9 }
    10 if (m_lienTrx.getPayeeId() <= 0) {
    11 throw EmsExceptionHelper.newValidationFailedException("PayeeId", "payeeId is missing");
    12 }
    13
    14 if (m_lienTrx.getExternalOrderId() == null) {
    15 throw EmsExceptionHelper.newValidationFailedException("ExternalOrderId", "external order id is missing");
    16 }
    17
    18 if (m_lienTrx.getExternalContractId() == null) {
    19 throw EmsExceptionHelper.newValidationFailedException("ExternalContractId", "external contract id is missing");
    20 }
    21 if (m_lienTrx.getExternalContractIdType() == null) {
    22 throw EmsExceptionHelper.newValidationFailedException("ExternalContractType", "external contract type is missing");
    23 }
    24
    25 if (m_lienTrx.getLienAmount() == null ) {
    26 throw EmsExceptionHelper.newValidationFailedException("LienAmount", "LienAmount is missing");
    27 } else if ( !m_lienTrx.getLienAmount().isPositive() ) {
    28 throw EmsExceptionHelper.newValidationFailedException("LienAmount", "LienAmount should be positive");
    29 }
    30
    31 }

    processInternally

    View Code
     1     protected void processInternally() throws Exception {
    2 //load emsTrxns
    3 loadEmsTrxns();
    4
    5 //check duplicate
    6 if(checkDuplicate()){
    7 return;
    8 }
    9
    10 //validate lien amount
    11 validateLienAmount();
    12
    13 //create lien
    14 createLien();
    15 }

    5.buildResponse

    View Code
    1     @Override
    2 protected void buildResponse() {
    3 EscrowLienTrx escrowLienTrx = new EscrowLienTrx(m_emsAdjTrx);
    4 m_response.setEscrowLienTrx(escrowLienTrx);
    5 EmsResponseCodeEnum resCode = m_isDuplicatedRequest ? EmsResponseCodeEnum.DUPLICTE_REQUEST_ERROR
    6 : EmsResponseCodeEnum.SUCCESS;
    7 m_response.setResponseCode(resCode);
    8 }







  • 相关阅读:
    Single Number II
    Pascal's Triangle
    Remove Duplicates from Sorted Array
    Populating Next Right Pointers in Each Node
    Minimum Depth of Binary Tree
    Unique Paths
    Sort Colors
    Swap Nodes in Pairs
    Merge Two Sorted Lists
    Climbing Stairs
  • 原文地址:https://www.cnblogs.com/littlesuccess/p/2285143.html
Copyright © 2011-2022 走看看