定义
策略模式定义一系列的算法, 将每一种算法封装起来并可以相互替换使用, 策略模式让算法独立于使用它的客户应用而独立变化.
在 Java 中, 策略模式的作用是将 if,else 和 switch 语句下的算法进行了单独封装, 使其成为单独的类而脱离了和客户应用的耦合.
JDK 中的策略模式
JDK 中的 ThreadPoolExecutor 使用了策略模式, 它预定了 4 中策略:
- ThreadPoolExecutor.AbortPolicy()
- ThreadPoolExecutor.CallerRunsPolicy()
- ThreadPoolExecutor.DiscardOldestPolicy()
- ThreadPoolExecutor.DiscardPolicy()
每种策略都被单独定义了一个类, 例如:
public static class AbortPolicy implements RejectedExecutionHandler
这符合对算法的单独封装, 并能够相互替换.
不使用策略模式的场景
例如支付场景下, 不使用策略模式的情况可能如下:
- /**
- * 支付类型
- */
- public enum PayType {
- ALI_PAY,
- WX_PAY,
- UNION_PAY;
- }
- /**
- * 测试类
- */
- public class StrategyTest {
- public static void main(String[] args) {
- PayType type = PayType.ALI_PAY;
- switch (type) {
- case ALI_PAY:
- System.out.println("使用支付宝付款..");
- break;
- case WX_PAY:
- System.out.println("使用微信付款..");
- break;
- case UNION_PAY:
- System.out.println("使用银联付款..");
- break;
- default:
- throw new RuntimeException("未知支付方式.");
- }
- }
- }
其中支付宝, 微信, 银联都是支付算法, 每种支付方式都能完成支付行为, 所以它们可以相互替换使用.
但是这里的支付算法都耦合在了客户应用中.
- package com.faith.NET.strategy.pay;
- /**
- * 普通支付接口
- */
- public interface CommonPay {
- public void pay(Integer money);
- }
- package com.faith.NET.strategy.pay;
- /**
- * 支付宝支付类
- */
- public class AliPay implements CommonPay {
- @Override
- public void pay(Integer money) {
- System.out.println("使用支付宝支付:" + money + "元.");
- }
- }
- package com.faith.NET.strategy.pay;
- /**
- * 微信支付类
- */
- public class WxPay implements CommonPay {
- @Override
- public void pay(Integer money) {
- System.out.println("使用微信支付:" + money + "元.");
- }
- }
- package com.faith.NET.strategy.pay;
- /**
- * 银联支付类
- */
- public class UnionPay implements CommonPay {
- @Override
- public void pay(Integer money) {
- System.out.println("使用银联支付:" + money + "元.");
- }
- }
- package com.faith.NET.strategy.pay;
- /**
- * 支付类型枚举
- */
- public enum PayType {
- ALI_PAY(new AliPay()),
- WX_PAY(new WxPay()),
- UNION_PAY(new UnionPay());
- // 每一个枚举实例都会拥有自己的 commonPay
- private CommonPay commonPay;
- PayType(CommonPay commonPay) {
- this.commonPay = commonPay;
- }
- public CommonPay get() {
- return this.commonPay;
- }
- }
- package com.faith.NET.strategy.pay;
- /**
- * 消费者类
- */
- public class Customer {
- public void pay(PayType payType, Integer money){
- payType.get().pay(money);
- }
- }
- package com.faith.NET.strategy.pay;
- /**
- * 测试类
- */
- public class StrategyTest {
- public static void main(String[] args) {
- Customer customer = new Customer();
- customer.pay(PayType.ALI_PAY, 100);
- }
- }
来源: https://yq.aliyun.com/articles/675857