最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java 策略模式與模板方法模式相關(guān)總結(jié)

 更新時間:2021年01月30日 09:49:52   作者:廢物大師兄  
這篇文章主要介紹了Java 策略模式與模板方法模式相關(guān)總結(jié),幫助大家更好的理解和使用Java,感興趣的朋友可以了解下

1.  策略模式

策略模式是一種行為設(shè)計(jì)模式,它能讓你定義一系列算法,并將每種算法分別放入獨(dú)立的類中,以使算法的對象能夠相互替換。

當(dāng)你有許多僅在執(zhí)行某些行為時略有不同的相似類時,可使用策略模式。使用該模式能將類的業(yè)務(wù)邏輯與其算法實(shí)現(xiàn)細(xì)節(jié)隔離開來。

說白了,其實(shí)還是解耦

策略模式的結(jié)構(gòu)如上圖所示,主要包含三個角色:

  • 抽象角色:通常是一個接口
  • 具體角色:接口的具體實(shí)現(xiàn)
  • 環(huán)境角色:調(diào)用接口的上下文環(huán)境,通常是一段業(yè)務(wù)邏輯方法

舉個常見的例子:支付

先定義一個接口 PayStrategy.java

package com.example.service;

import com.example.domain.dto.PayDTO;
import com.example.domain.dto.PayDetailDTO;

/**
 * @author ChengJianSheng
 * @date 2021/1/11
 */
public interface PayStrategy {

  /**
   * 下單
   */
  PayDTO prepay();

  /**
   * 查詢
   */
  PayDetailDTO query();

  /**
   * 撤銷
   */
  void cancel();

  /**
   * 退款
   */
  void refund();

} 

然后是具體實(shí)現(xiàn)

AlipayStrategy.java 

package com.example.service.impl;

import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradeCancelResponse;
import com.example.domain.dto.PayDTO;
import com.example.domain.dto.PayDetailDTO;
import com.example.service.PayStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * https://opendocs.alipay.com/open/common/abilitymap
 * https://opendocs.alipay.com/open/194/106078
 * 掃碼支付
 */
@Component
public class AlipayStrategy implements PayStrategy {

  @Autowired
  private AlipayClient alipayClient;

  @Override
  public PayDTO prepay() {
    AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
    AlipayTradeCancelResponse response = alipayClient.execute(request);
    return null;
  }

  @Override
  public PayDetailDTO query() {
    return null;
  }

  @Override
  public void cancel() {

  }

  @Override
  public void refund() {

  }

  public void payNotify(String data) {

  }

  public void refundNotify() {

  }
}

WeixinPayStrategy.java

package com.example.service.impl;

import com.example.domain.dto.PayDTO;
import com.example.domain.dto.PayDetailDTO;
import com.example.service.PayStrategy;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.request.WxPayOrderQueryRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.service.WxPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pages/index.shtml
 * https://github.com/Wechat-Group/WxJava/wiki/%E5%BE%AE%E4%BF%A1%E6%94%AF%E4%BB%98
 * @author ChengJianSheng
 * @date 2021/1/11
 */
@Component
public class WeixinPayStrategy implements PayStrategy {

  @Autowired
  private WxPayService wxPayService;

  @Override
  public PayDTO prepay() {
    WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
    wxPayService.createOrder(request);
    return null;
  }

  @Override
  public PayDetailDTO query() {
    WxPayOrderQueryRequest request = new WxPayOrderQueryRequest();
    wxPayService.queryOrder(request);
    return null;
  }

  @Override
  public void cancel() {

  }

  @Override
  public void refund() {

  }

  public void payNotify(String data) {
    WxPayOrderNotifyResult result = wxPayService.parseOrderNotifyResult(data);
  }

  public void refundNotify(String data) {
    WxPayOrderNotifyResult result = wxPayService.parseRefundNotifyResult(data);
  }
} 

上下文

package com.example.service.impl;

import com.example.domain.dto.PayDTO;
import com.example.service.PayService;
import com.example.service.PayStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author ChengJianSheng
 * @date 2021/1/11
 */
@Service
public class PayServiceImpl implements PayService {
  @Autowired
  private AlipayStrategy alipayStrategy;
  @Autowired
  private WeixinPayStrategy weixinPayStrategy;

  @Override
  public void prePay(PayDTO payDTO) {
    // 創(chuàng)建支付訂單
    // 組裝參數(shù)
    PayStrategy payStrategy = null;
    if (payDTO.getChannel() == 1) {
      payStrategy = alipayStrategy;
    } else {
      payStrategy = weixinPayStrategy;
    }

    payStrategy.prepay();

  }
} 

這樣就將算法的細(xì)節(jié)與業(yè)務(wù)邏輯隔離開,開發(fā)始終要遵循的原則是:高內(nèi)聚,低耦合

其余部分代碼補(bǔ)充如下:

pom.xml

<dependency>
  <groupId>com.alipay.sdk</groupId>
  <artifactId>alipay-sdk-java</artifactId>
  <version>4.11.8.ALL</version>
</dependency>
<dependency>
  <groupId>com.github.binarywang</groupId>
  <artifactId>weixin-java-pay</artifactId>
  <version>4.0.0</version>
</dependency>

AlipayConfig.java

package com.example.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 掃碼支付
 * https://opendocs.alipay.com/open/194/106078
 * https://opendocs.alipay.com/open/common/abilitymap
 *
 * @author ChengJianSheng
 * @date 2021/1/11
 */
@Configuration
public class AlipayConfig {
  @Value("${alipay.appId}")
  private String appId;
  @Value("${alipay.privateKey}")
  private String privateKey;
  @Value("${alipay.publicKey}")
  private String publicKey;

  @Bean
  public AlipayClient alipayClient() {
    AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", publicKey, "RSA2");
    return alipayClient;
  }
}

WeixinPayConfig.java 

package com.example.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * https://pay.weixin.qq.com/wiki/doc/apiv3/index.shtml
 * https://github.com/Wechat-Group/WxJava/wiki/%E5%BE%AE%E4%BF%A1%E6%94%AF%E4%BB%98
 * @author ChengJianSheng
 * @date 2021/1/11
 */
@Configuration
public class WeixinPayConfig {
  /**
   * 公眾號appid
   */
  @Value("${weixin.pay.appId}")
  private String appId;
  /**
   * 商戶號.
   */
  @Value("${weixin.pay.mchId}")
  private String mchId;
  /**
   * 商戶密鑰.
   */
  @Value("${weixin.pay.mchKey}")
  private String mchKey;

  @Value("${weixin.pay.notifyUrl}")
  private String notifyUrl;

  @Bean
  public WxPayService wxPayService() {
    WxPayConfig payConfig = new WxPayConfig();
    payConfig.setAppId(appId);
    payConfig.setMchId(mchId);
    payConfig.setMchKey(mchKey);
    payConfig.setNotifyUrl(notifyUrl);

    WxPayService wxPayService = new WxPayServiceImpl();
    wxPayService.setConfig(payConfig);
    return wxPayService;
  }
}

2.  模板方法模式

模板方法模式是一種行為設(shè)計(jì)模式,它在超類中定義了一個算法的框架,允許子類在不修改結(jié)構(gòu)的情況下重寫算法的特定步驟。 

當(dāng)多個類的算法除一些細(xì)微不同之外幾乎完全一樣時,可使用該模式。

這里,“算法”應(yīng)理解為一個功能,或者一段業(yè)務(wù)邏輯 

模板方法模式的結(jié)構(gòu)如上圖所示,主要實(shí)現(xiàn)方式是

  1. 將一些公共的邏輯抽象出來,將功能實(shí)現(xiàn)分解為多個步驟
  2. 定義抽象類,將有差異的步驟聲明為抽象方法
  3. 子類繼承抽象基類,實(shí)現(xiàn)其中的抽象方法 

模板方法減少了重復(fù)代碼,將公共代碼提到基類中,子類只需關(guān)注各自差異化的邏輯 

上面的支付,也可以用模板方法模式來實(shí)現(xiàn)。

個人覺得,策略模式、工廠方法模式、模板方法模式,這三個都比較像。能用模板方法模式的地方,通常也可以用策略模式。

只是它們的側(cè)重點(diǎn)不一樣,策略模式的側(cè)重點(diǎn)在于可以動態(tài)切換算法,即同樣的參數(shù),用不同的策略執(zhí)行,可以得到不同的結(jié)果。

而模板方法模式的側(cè)重點(diǎn)在于算法結(jié)構(gòu)不變,中間的某些步驟的具體實(shí)現(xiàn)可以不同。

如果我們把策略模式中的上下文看成一個算法的話,那策略模式中的具體實(shí)現(xiàn)就是特定的步驟,這么一想,感覺二者太像了。

模板方法模式有一個活生生的例子是java.io.InputStream。InputStream中定義了一個抽象的read()方法,從流中讀取數(shù)據(jù)的方法時一樣的,只是從什么流中讀取的問題,可以從文件流中讀,也可以從網(wǎng)絡(luò)流中讀。

最后,不要為了用設(shè)計(jì)模式而用設(shè)計(jì)模式。

以上就是Java 策略模式與模板方法模式相關(guān)總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Java 策略模式與模板方法模式的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 貪心算法原理及在Java中的使用

    貪心算法原理及在Java中的使用

    我們可能在好多地方都會聽到貪心算法這一概念,并且它的算法思想也比較簡單就是說算法只保證局部最優(yōu),進(jìn)而達(dá)到全局最優(yōu)。但我們實(shí)際編程的過程中用的并不是很多,究其原因可能是貪心算法使用的條件比較苛刻,所要解決的問題必須滿足貪心選擇性質(zhì)
    2021-05-05
  • springboot的application.yml配置port不生效的解決方案

    springboot的application.yml配置port不生效的解決方案

    這篇文章主要介紹了springboot的application.yml配置port不生效的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • MyBatis-Plus與Druid結(jié)合Dynamic-datasource實(shí)現(xiàn)多數(shù)據(jù)源操作數(shù)據(jù)庫的示例

    MyBatis-Plus與Druid結(jié)合Dynamic-datasource實(shí)現(xiàn)多數(shù)據(jù)源操作數(shù)據(jù)庫的示例

    Dynamic-DataSource 可以和絕大多是連接層插件搭配使用,比如:mybatis,mybatis-plus,hibernate等,本文就來介紹一下MyBatis-Plus與Druid結(jié)合Dynamic-datasource實(shí)現(xiàn)多數(shù)據(jù)源操作數(shù)據(jù)庫的示例,感興趣的可以了解一下
    2023-10-10
  • spring boot日志管理配置

    spring boot日志管理配置

    這篇文章主要介紹了spring boot日志管理配置的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Java并發(fā)編程——volatile關(guān)鍵字

    Java并發(fā)編程——volatile關(guān)鍵字

    這篇文章主要介紹了Java并發(fā)編程——volatile關(guān)鍵字的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java并發(fā)編程,感興趣的朋友可以了解下
    2020-10-10
  • java 數(shù)據(jù)結(jié)構(gòu)二叉樹的實(shí)現(xiàn)代碼

    java 數(shù)據(jù)結(jié)構(gòu)二叉樹的實(shí)現(xiàn)代碼

    這篇文章主要介紹了java 數(shù)據(jù)結(jié)構(gòu)二叉樹的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • 淺談Java三目運(yùn)算

    淺談Java三目運(yùn)算

    本文給大家主要介紹的是java中三目運(yùn)算的詳細(xì)介紹,并附上2個示例,希望對大家理解三目運(yùn)算能夠有所幫助。
    2015-03-03
  • 使用SpringSecurity+defaultSuccessUrl不跳轉(zhuǎn)指定頁面的問題解決方法

    使用SpringSecurity+defaultSuccessUrl不跳轉(zhuǎn)指定頁面的問題解決方法

    本人是用springsecurity的新手,今天遇到defaultSuccessUrl不跳轉(zhuǎn)指定頁面的問題,真是頭疼死了,網(wǎng)上找遍了解決方法都解決不了,今天給大家分享使用SpringSecurity+defaultSuccessUrl不跳轉(zhuǎn)指定頁面的問題解決方法,感興趣的朋友一起看看吧
    2023-12-12
  • Java 數(shù)據(jù)庫連接池 Tomcat介紹

    Java 數(shù)據(jù)庫連接池 Tomcat介紹

    這篇文章主要給大家分享了 Java 數(shù)據(jù)庫連接池 Tomcat介紹,omcat 是一個小型的輕量級應(yīng)用服務(wù)器,在中小型系統(tǒng)和并發(fā)訪問用戶不是很多的場合下被普遍使用,是開發(fā)和調(diào)試JSP 程序的首選。下面來看看文章內(nèi)容的詳細(xì)介紹吧
    2021-11-11
  • springboot項(xiàng)目中jackson-序列化-處理 NULL教程

    springboot項(xiàng)目中jackson-序列化-處理 NULL教程

    這篇文章主要介紹了springboot項(xiàng)目中jackson-序列化-處理 NULL教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10

最新評論

亚东县| 措勤县| 秭归县| 锡林浩特市| 金阳县| 湟源县| 四平市| 平乡县| 房产| 大港区| 阿瓦提县| 喀喇沁旗| 屏东县| 阿拉尔市| 昭苏县| 囊谦县| 汉寿县| 陆良县| 常德市| 宣化县| 吴川市| 凤阳县| 乌鲁木齐县| 和顺县| 勐海县| 盱眙县| 宁明县| 黎城县| 龙门县| 永年县| 南充市| 新乐市| 五莲县| 理塘县| 石泉县| 肇州县| 郯城县| 龙州县| 鲁甸县| 鄂托克前旗| 钟山县|