国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

瀏覽:10日期:2022-08-17 14:59:47
1. 策略模式

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

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

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

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

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

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

舉個(gè)常見(jiàn)的例子:支付

先定義一個(gè)接口 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 * 掃碼支付 */@Componentpublic 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 */@Componentpublic 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 */@Servicepublic 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ù)邏輯隔離開(kāi),開(kāi)發(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 */@Configurationpublic 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 */@Configurationpublic class WeixinPayConfig { /** * 公眾號(hào)appid */ @Value('${weixin.pay.appId}') private String appId; /** * 商戶號(hào). */ @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ì)模式,它在超類中定義了一個(gè)算法的框架,允許子類在不修改結(jié)構(gòu)的情況下重寫算法的特定步驟。

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

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

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

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

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

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

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

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

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

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

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

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

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

以上就是Java 策略模式與模板方法模式相關(guān)總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Java 策略模式與模板方法模式的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 德格县| 绥江县| 南京市| 平顶山市| 胶州市| 青田县| 合水县| 交城县| 大英县| 炉霍县| 镇坪县| 宿迁市| 盐山县| 曲麻莱县| 建德市| 永德县| 石门县| 龙海市| 冕宁县| 大洼县| 孝昌县| 美姑县| 三台县| 鹤庆县| 呼伦贝尔市| 内丘县| 晋江市| 安庆市| 抚顺市| 永顺县| 龙泉市| 南充市| 汤阴县| 太原市| 松桃| 遂昌县| 千阳县| 白山市| 老河口市| 任丘市| 新泰市|