经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » 设计模式 » 查看文章
策略模式与模板方法模式
来源:cnblogs  作者:废物大师兄  时间:2021/1/18 16:28:05  对本文有异议

1.  策略模式

策略模式是一种行为设计模式,它能让你定义一系列算法,并将每种算法分别放入独立的类中,以使算法的对象能够相互替换。

当你有许多仅在执行某些行为时略有不同的相似类时,可使用策略模式。使用该模式能将类的业务逻辑与其算法实现细节隔离开来。

说白了,其实还是解耦

策略模式的结构如上图所示,主要包含三个角色:

  • 抽象角色:通常是一个接口
  • 具体角色:接口的具体实现
  • 环境角色:调用接口的上下文环境,通常是一段业务逻辑方法

举个常见的例子:支付

先定义一个接口 PayStrategy.java

  1. package com.example.service;
  2. import com.example.domain.dto.PayDTO;
  3. import com.example.domain.dto.PayDetailDTO;
  4. /**
  5. * @author ChengJianSheng
  6. * @date 2021/1/11
  7. */
  8. public interface PayStrategy {
  9. /**
  10. * 下单
  11. */
  12. PayDTO prepay();
  13. /**
  14. * 查询
  15. */
  16. PayDetailDTO query();
  17. /**
  18. * 撤销
  19. */
  20. void cancel();
  21. /**
  22. * 退款
  23. */
  24. void refund();
  25. } 

然后是具体实现

AlipayStrategy.java 

  1. package com.example.service.impl;
  2. import com.alipay.api.AlipayClient;
  3. import com.alipay.api.request.AlipayTradePrecreateRequest;
  4. import com.alipay.api.response.AlipayTradeCancelResponse;
  5. import com.example.domain.dto.PayDTO;
  6. import com.example.domain.dto.PayDetailDTO;
  7. import com.example.service.PayStrategy;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Component;
  10. /**
  11. * https://opendocs.alipay.com/open/common/abilitymap
  12. * https://opendocs.alipay.com/open/194/106078
  13. * 扫码支付
  14. */
  15. @Component
  16. public class AlipayStrategy implements PayStrategy {
  17. @Autowired
  18. private AlipayClient alipayClient;
  19. @Override
  20. public PayDTO prepay() {
  21. AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
  22. AlipayTradeCancelResponse response = alipayClient.execute(request);
  23. return null;
  24. }
  25. @Override
  26. public PayDetailDTO query() {
  27. return null;
  28. }
  29. @Override
  30. public void cancel() {
  31. }
  32. @Override
  33. public void refund() {
  34. }
  35. public void payNotify(String data) {
  36. }
  37. public void refundNotify() {
  38. }
  39. }

WeixinPayStrategy.java

  1. package com.example.service.impl;
  2. import com.example.domain.dto.PayDTO;
  3. import com.example.domain.dto.PayDetailDTO;
  4. import com.example.service.PayStrategy;
  5. import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
  6. import com.github.binarywang.wxpay.bean.request.WxPayOrderQueryRequest;
  7. import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
  8. import com.github.binarywang.wxpay.service.WxPayService;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Component;
  11. /**
  12. * https://pay.weixin.qq.com/wiki/doc/apiv3/wxpay/pages/index.shtml
  13. * https://github.com/Wechat-Group/WxJava/wiki/%E5%BE%AE%E4%BF%A1%E6%94%AF%E4%BB%98
  14. * @author ChengJianSheng
  15. * @date 2021/1/11
  16. */
  17. @Component
  18. public class WeixinPayStrategy implements PayStrategy {
  19. @Autowired
  20. private WxPayService wxPayService;
  21. @Override
  22. public PayDTO prepay() {
  23. WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
  24. wxPayService.createOrder(request);
  25. return null;
  26. }
  27. @Override
  28. public PayDetailDTO query() {
  29. WxPayOrderQueryRequest request = new WxPayOrderQueryRequest();
  30. wxPayService.queryOrder(request);
  31. return null;
  32. }
  33. @Override
  34. public void cancel() {
  35. }
  36. @Override
  37. public void refund() {
  38. }
  39. public void payNotify(String data) {
  40. WxPayOrderNotifyResult result = wxPayService.parseOrderNotifyResult(data);
  41. }
  42. public void refundNotify(String data) {
  43. WxPayOrderNotifyResult result = wxPayService.parseRefundNotifyResult(data);
  44. }
  45. } 

上下文

  1. package com.example.service.impl;
  2. import com.example.domain.dto.PayDTO;
  3. import com.example.service.PayService;
  4. import com.example.service.PayStrategy;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. /**
  8. * @author ChengJianSheng
  9. * @date 2021/1/11
  10. */
  11. @Service
  12. public class PayServiceImpl implements PayService {
  13. @Autowired
  14. private AlipayStrategy alipayStrategy;
  15. @Autowired
  16. private WeixinPayStrategy weixinPayStrategy;
  17. @Override
  18. public void prePay(PayDTO payDTO) {
  19. // 创建支付订单
  20. // 组装参数
  21. PayStrategy payStrategy = null;
  22. if (payDTO.getChannel() == 1) {
  23. payStrategy = alipayStrategy;
  24. } else {
  25. payStrategy = weixinPayStrategy;
  26. }
  27. payStrategy.prepay();
  28. }
  29. } 

这样就将算法的细节与业务逻辑隔离开,开发始终要遵循的原则是:高内聚,低耦合

其余部分代码补充如下:

pom.xml

  1. <dependency>
  2. <groupId>com.alipay.sdk</groupId>
  3. <artifactId>alipay-sdk-java</artifactId>
  4. <version>4.11.8.ALL</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.github.binarywang</groupId>
  8. <artifactId>weixin-java-pay</artifactId>
  9. <version>4.0.0</version>
  10. </dependency>

 AlipayConfig.java

  1. package com.example.config;
  2. import com.alipay.api.AlipayClient;
  3. import com.alipay.api.DefaultAlipayClient;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. /**
  8. * 扫码支付
  9. * https://opendocs.alipay.com/open/194/106078
  10. * https://opendocs.alipay.com/open/common/abilitymap
  11. *
  12. * @author ChengJianSheng
  13. * @date 2021/1/11
  14. */
  15. @Configuration
  16. public class AlipayConfig {
  17. @Value("${alipay.appId}")
  18. private String appId;
  19. @Value("${alipay.privateKey}")
  20. private String privateKey;
  21. @Value("${alipay.publicKey}")
  22. private String publicKey;
  23. @Bean
  24. public AlipayClient alipayClient() {
  25. AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, privateKey, "json", "UTF-8", publicKey, "RSA2");
  26. return alipayClient;
  27. }
  28. }

WeixinPayConfig.java 

  1. package com.example.config;
  2. import com.github.binarywang.wxpay.config.WxPayConfig;
  3. import com.github.binarywang.wxpay.service.WxPayService;
  4. import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. /**
  9. * https://pay.weixin.qq.com/wiki/doc/apiv3/index.shtml
  10. * https://github.com/Wechat-Group/WxJava/wiki/%E5%BE%AE%E4%BF%A1%E6%94%AF%E4%BB%98
  11. * @author ChengJianSheng
  12. * @date 2021/1/11
  13. */
  14. @Configuration
  15. public class WeixinPayConfig {
  16. /**
  17. * 公众号appid
  18. */
  19. @Value("${weixin.pay.appId}")
  20. private String appId;
  21. /**
  22. * 商户号.
  23. */
  24. @Value("${weixin.pay.mchId}")
  25. private String mchId;
  26. /**
  27. * 商户密钥.
  28. */
  29. @Value("${weixin.pay.mchKey}")
  30. private String mchKey;
  31. @Value("${weixin.pay.notifyUrl}")
  32. private String notifyUrl;
  33. @Bean
  34. public WxPayService wxPayService() {
  35. WxPayConfig payConfig = new WxPayConfig();
  36. payConfig.setAppId(appId);
  37. payConfig.setMchId(mchId);
  38. payConfig.setMchKey(mchKey);
  39. payConfig.setNotifyUrl(notifyUrl);
  40. WxPayService wxPayService = new WxPayServiceImpl();
  41. wxPayService.setConfig(payConfig);
  42. return wxPayService;
  43. }
  44. }

2.  模板方法模式

模板方法模式是一种行为设计模式,它在超类中定义了一个算法的框架,允许子类在不修改结构的情况下重写算法的特定步骤。 

当多个类的算法除一些细微不同之外几乎完全一样时,可使用该模式。

这里,“算法”应理解为一个功能,或者一段业务逻辑 

模板方法模式的结构如上图所示,主要实现方式是

  1. 将一些公共的逻辑抽象出来,将功能实现分解为多个步骤
  2. 定义抽象类,将有差异的步骤声明为抽象方法
  3. 子类继承抽象基类,实现其中的抽象方法 

模板方法减少了重复代码,将公共代码提到基类中,子类只需关注各自差异化的逻辑 

上面的支付,也可以用模板方法模式来实现。

个人觉得,策略模式、工厂方法模式、模板方法模式,这三个都比较像。能用模板方法模式的地方,通常也可以用策略模式。

只是它们的侧重点不一样,策略模式的侧重点在于可以动态切换算法,即同样的参数,用不同的策略执行,可以得到不同的结果。

而模板方法模式的侧重点在于算法结构不变,中间的某些步骤的具体实现可以不同。

如果我们把策略模式中的上下文看成一个算法的话,那策略模式中的具体实现就是特定的步骤,这么一想,感觉二者太像了。

模板方法模式有一个活生生的例子是java.io.InputStream。InputStream中定义了一个抽象的read()方法,从流中读取数据的方法时一样的,只是从什么流中读取的问题,可以从文件流中读,也可以从网络流中读。

最后,不要为了用设计模式而用设计模式

https://refactoringguru.cn/design-patterns

原文链接:http://www.cnblogs.com/cjsblog/p/14272184.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号