策略模式

本文深入解析策略模式,通过支付场景示例,展示了如何利用策略模式解耦算法与客户端,避免if/else和switch语句的过度使用,提高代码的可维护性和灵活性。

定义

策略模式定义一系列的算法,将每一种算法封装起来并可以相互替换使用,策略模式让算法独立于使用它的客户应用而独立变化。

在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;
    }
}

构造一个消费者类,其拥有支付pay方法。

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);
    }
}

使用策略模式后,成功的避免了if/else和switch。对算法和客户应用程序进行了解耦。

很多例子使用的商场促销的算法,例如满减,打折等,其中满减、打折都属于算法,可以将满减和打折这些算法都单独封装为算法类。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值