• 设计模式之——策略模式


    策略模式介绍

    • 定义多个算法,将每一个算法封装起来,从而使它们可以相互切换;

    什么时候使用策略模式

    • 超过 3 层的 if-else 的逻辑判断代码可以使用卫语句、策略模式、状态模式等来实现:(出自阿里开发规约)
    if (conditionA) {
       // 逻辑1
    } else if (conditionB) {
        // 逻辑2
    } else if (conditionC) {
        // 逻辑3
    } else {
        // 逻辑4
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    以上代码违反了开闭原则,具体定义看这里:设计模式七大原则

    优点

    • 多重判断语句不易维护(if else),使用策略模式可以避免多重条件判断语句;
    • 供了对开闭原则的支持,可以在不修改原代码的情况下,增加新的算法;

    缺点

    • 使用策略模式会造成很多策略类,在一定程度上增加了维护难度;
    • 客户端必须知道所有的策略类,并自行决定使用哪一个策略类;

    定义

    • 定义策略接口:定义一个接口,各种不同算法类都需要实现此接口,起到规范作用;
    • 策略类:具体的策略类,实现策略接口,并实现不同算法;
    • 环境类:持有一个策略类的引用,最终给客户端调用;

    模拟场景

    • 商城根据用户等级,进行优惠活动;
    • 用户分为普通用户(不打折)、普通会员(9折)以及高级会员(8折);

    创建策略接口

    /**
     * @author 岳晓鵬
     * @version 1.0
     * @date 2022-06-26 11:53
     */
    public interface Strategy {
    	
    	 /**
         * 计算优惠价格
         * @param price
         * @return
         */
        BigDecimal discount(BigDecimal price);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    创建多个策略类

    /**
     * @author 岳晓鵬
     * @version 1.0
     * @date 2022-06-26 11:53
     */
    public class ConcreteStrategyA implements Strategy {
    
        @Override
        public BigDecimal discount(BigDecimal price) {
            System.out.println("普通用户,不打折");
            return price;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    /**
     * @author 岳晓鵬
     * @version 1.0
     * @date 2022-06-26 11:53
     */
    public class ConcreteStrategyB implements Strategy {
    
        @Override
        public BigDecimal discount(BigDecimal price) {
            System.out.println("普通会员,打9折");
            return price.multiply(new BigDecimal("0.9"));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    /**
     * @author 岳晓鵬
     * @version 1.0
     * @date 2022-06-26 11:53
     */
    public class ConcreteStrategyC implements Strategy {
    
        @Override
        public BigDecimal discount(BigDecimal price) {
            System.out.println("高级会员,打8折");
            return price.multiply(new BigDecimal("0.8"));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    创建环境类

    在此调用策略

    public class Context {
    
        private Strategy strategy;
    
        // 初始化时,传入具体的策略对象
        public Context(Strategy strategy) {
            this.strategy = strategy;
        }
    
        // 根据具体的策略对象,调用其算法的对象
        public BigDecimal contextInterface(BigDecimal price) {
            return strategy.discount(price);
        }
    
    
        public static void main(String[] args) {
            Context context = null;
            
            // 普通用户
            context = new Context(new ConcreteStrategyA());
            System.out.println(context.contextInterface(new BigDecimal("100")));
    
            // 普通会员
            context = new Context(new ConcreteStrategyB());
            System.out.println(context.contextInterface(new BigDecimal("100")));
    
            // 高级会员
            context = new Context(new ConcreteStrategyC());
            System.out.println(context.contextInterface(new BigDecimal("100")));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    打印结果

    普通用户,不打折
    100
    普通会员,打990.0
    高级会员,打880.0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 相关阅读:
    Word中设置粘贴为纯文本的自定义快捷键
    使用 COPY 加速 PostgreSQL 批量插入
    Go学习第七章——数组arr,切片slice和映射map
    STM32 BSRR BRR ODR 寄存器解析(F4系列已经去掉BRR寄存器了)
    Linux SysRq 简介
    2023年9月国产数据库大事记-墨天轮
    SpringBoot 日志
    WooCommerce客户数据如何存储在数据库中
    ROS读书记录1:机器人SLAM导航核心技术与实战1
    @SpringBootApplication注解说明(InsCode AI 创作助手)
  • 原文地址:https://blog.csdn.net/xianyun1992/article/details/125448164