• 理解策略模式


    策略模式

    策略模式的理解就是当代码里面的某个地方遇到了多种选择,多种方案,多种可能性的时候。最笨的方法就是针对不同的选择 做if-else,然后分别选择各种方案。

    if(A){
    方案A
    }if else(B){
    方案B
    }
    if else(C){
    方案C
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    但是这样代码会非常的臃肿和不好维护,因为所有的方案都写到了一个方法里面。


    策略模式就是把这些方案使用独立的类 封装起来,有多少个方案就有多少个类。 所有的方案实现一个统一的接口(方案的总和)。

    比如网上常见的经典的 ”付款折扣模型“

    付款是一个方案的总和,付款下面有各种方案 比如 一折 三折 五折 八折 。。。。
    注意策略模式里面这些一个个方案都是独立 互斥的,你只能多选一。

    代码示例

    先来一个抽象的统一接口 ——付款方式
    (这个接口就是宏观上对所有方案的总结)

    public interface Buyer {
        BigDecimal calPrice(BigDecimal orderPrice);
    }
    
    • 1
    • 2
    • 3

    然后我们针对每个方案创建一个类 (替换原有的if-else)
    (这里也可以发现 如果方案特别特别多的话 策略模式代码还是挺麻烦的)

    public class VIP1 implements Buyer {
        @Override
        public BigDecimal calPrice(BigDecimal orderPrice) {
            一折计算
        }
    }
    public class VIP2 implements Buyer {
        @Override
        public BigDecimal calPrice(BigDecimal orderPrice) {
           二折计算
        }
    }
    public class VIP3 implements Buyer {
        @Override
        public BigDecimal calPrice(BigDecimal orderPrice) {
         三折计算
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    接下来主要的一步就是 “判断”, 现在已经有了三种 付款方案 实现了抽象 付款接口。
    那么客户端调用的时候 怎么样根据客户端的需求 选择对应的方案:

    public class Cashier {
        private Buyer buyer;
        public Cashier(Buyer buyer) {
            this.buyer = buyer;
        }
        public BigDecimal qupta(BigDecimal orderPrice) {
            return this.buyer.calPrice(orderPrice);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    这里我们创建一个上下文收银类 cashier。

    cashier 定义1个属性 Buyer (注意 这里Buyer是一个接口,而不是类。)

    在它的构造函数中 对 Buyer 进行赋值,这里利用了java中的多态特性,实现了同一个接口的不同实现。


    测试

            Cashier ex = new Cashier(new VIP1());
            System.out.println(ex.quota(new BigDecimal(100)));
    
            Cashier svip = new Cashier(new VIP2());
            System.out.println(svip.quota(new BigDecimal(100)));
    
            Cashier vip = new Cashier(new VIP3());
            System.out.println(vip.quota(new BigDecimal(100)));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    Java反射原理和实际用法
    MySQL找回误删的数据,数据恢复
    235. 二叉搜索树的最近公共祖先
    【MATLAB】【函数介绍】cp2tform函数从控制点对推断空间变换
    MySQL线程池
    使用Python的requests库采集充电桩LBS位置经纬度信息
    网络安全(黑客)技术——自学2024
    什么是HTTP/2?它与HTTP/1.1相比有什么改进?
    Maven 命令行环境
    【软考的故事】软考从泄题风波到机考改革,是何原委?
  • 原文地址:https://blog.csdn.net/weixin_43966468/article/details/133037879