工廠模式-微信支付、支付寶支付

當我們做支付項目時,常常項目中需要接入多種支付方式(微信、支付寶、銀聯等)。
其實這些支付方式,都存在一些類似的業務接口,比如支付、查詢、退款等等。
區別就在於每一家參數不一致(參數名不一致)。我們可以利用工廠模式將支付廠商封裝起來。
利用工廠模式,我們可以更方便的選擇支付方式接口。


下面代碼僅僅是結構,暫無具體的支付實現。

下面代碼以java代碼爲例,工廠模式與編程語言無關,其他編程語言也可以,如C# , Go。

代碼中類似ResultCreatePay、CreatePayParas僅僅是命名,暫無具體屬性。


一、創建接口

/**   支付廠商(微信、支付寶、銀聯等)公共支付接口
*/ 
public interface Pay {

   /** 1.創建支付
    */ 
   ResultCreatePay CreatePay(CreatePayParas createPayParas);

   /** 2.支付後,支付廠商回調通知結果處理
    */ 
   ResultPayNotify PayNotify(PayNotifyParas payNotifyParas);

   /** 3.查詢支付結果狀態
    */ 
   ResultQueryPayStatus QueryPayStatus(QueryPayStatusParas queryPayStatusParas);

   /** 4.創建退款
    */ 
   ResultCreateRefund CreateRefund(CreateRefundParas createRefundParas);

   /** 5.退款後,支付廠商回調通知結果處理
    */ 
   ResultRefundNotify RefundNotify(RefundNotifyParas refundNotifyParas);

   /*** 其他公共接口,暫不一一列舉  ***/
}

二、實現接口的實體類

微信支付:

/** 支付廠商(微信)支付相關接口實現
*/ 
public class WeiXinPay implements Pay {

   /** 1.創建支付
    */ 
   @Override 
   public ResultCreatePay CreatePay(CreatePayParas createPayParas){
       //實現微信接口
       return new ResultCreatePay() 
   }

   /** 2.支付後,支付廠商回調通知結果處理
    */
   @Override 
   public ResultPayNotify PayNotify(PayNotifyParas payNotifyParas){
       //實現微信接口
       return new ResultPayNotify() 
   }

   /** 3.查詢支付結果狀態
    */
   @Override 
   public ResultQueryPayStatus QueryPayStatus(QueryPayStatusParas queryPayStatusParas){
       //實現微信接口
       return new ResultQueryPayStatus() 
   }

   /** 4.創建退款
    */
   @Override   
   public ResultCreateRefund CreateRefund(CreateRefundParas createRefundParas){
       //實現微信接口
       return new ResultCreateRefund() 
   }

   /** 5.退款後,支付廠商回調通知結果處理
    */
   @Override  
   public ResultRefundNotify RefundNotify(RefundNotifyParas refundNotifyParas){
       //實現微信接口
       return new ResultRefundNotify() 
   }
}

支付寶支付:

/** 支付廠商(支付寶)支付相關接口實現
*/ 
public class ALiPay implements Pay {

   /** 1.創建支付
    */ 
   @Override 
   public ResultCreatePay CreatePay(CreatePayParas createPayParas){
       //實現支付寶接口
       return new ResultCreatePay() 
   }

   /** 2.支付後,支付廠商回調通知結果處理
    */
   @Override 
   public ResultPayNotify PayNotify(PayNotifyParas payNotifyParas){
       //實現支付寶接口
       return new ResultPayNotify() 
   }

   /** 3.查詢支付結果狀態
    */
   @Override 
   public ResultQueryPayStatus QueryPayStatus(QueryPayStatusParas queryPayStatusParas){
       //實現支付寶接口
       return new ResultQueryPayStatus() 
   }

   /** 4.創建退款
    */
   @Override   
   public ResultCreateRefund CreateRefund(CreateRefundParas createRefundParas){
       //實現支付寶接口
       return new ResultCreateRefund() 
   }

   /** 5.退款後,支付廠商回調通知結果處理
    */
   @Override  
   public ResultRefundNotify RefundNotify(RefundNotifyParas refundNotifyParas){
       //實現支付寶接口
       return new ResultRefundNotify() 
   }
}

三、創建工廠,生成基於特定類型的實體類

public class PayFactory {
    
   public Pay getPay(String payType){
      if(payType.equals("ALiPay")){
         return new ALiPay();
      } else if(payType.equals("WeiXinPay")){
         return new WeiXinPay();
      } 

      return null;
   }
}

四、使用工廠,通過傳遞支付類型來獲取實體類的對象

public class Demo {
 
   public static void main(String[] args) {
      PayFactory payFactory = new PayFactory();
 
      //獲取支付寶支付
      Pay aLiPay = payFactory.getPay("ALiPay");
      //調用aLiPay中的支付、查詢、退款等等  
      ResultCreatePay  result1 = aLiPay.CreatePay(new CreatePayParas());

      //獲取微信支付   
      Pay weiXinPay = payFactory.getPay("WeiXinPay");
      //調用weiXinPay中的支付、查詢、退款等等  
      ResultCreatePay  result1 = weiXinPay.CreatePay(new CreatePayParas());
      
   }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章