• 设计模式之策略模式


    策略模式(Strategy Pattern)

    定义

    定义一系列算法,封装每个算法,并使它们可以互换。

    使用场景

    主要角色

    类图

    image-20231224153459958

    示例代码

    //抽象策略角色
    public interface Strategy {
        //策略模式的运算法则
        void doSomething();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    //具体策略角色
    public class ConcreteStrategy1 implements Strategy {
        @Override
        public void doSomething() {
            System.out.println("具体策略1的运算法则...");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    //具体策略角色
    public class ConcreteStrategy2 implements Strategy {
        @Override
        public void doSomething() {
            System.out.println("具体策略2的运算法则...");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    //封装角色
    public class Context {
        //抽象策略
        private Strategy strategy;
    
        //构造函数设置具体策略
        public Context(Strategy strategy) {
            this.strategy = strategy;
        }
    
        //封装后的策略方法
        public void doAnything() {
            this.strategy.doSomething();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    public class Client {
        public static void main(String[] args) {
            //拿到一个具体的策略
            Strategy strategy = new ConcreteStrategy1();
            //创建上下文对象
            Context context = new Context(strategy);
            //执行封装后的方法
            context.doAnything();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    使用Spring实现策略模式+工厂模式

    public interface Strategy {
        //策略模式的运算法则
        void doSomething();
    }
    
    • 1
    • 2
    • 3
    • 4
    @Component("concreteStrategy1")
    public class ConcreteStrategy1 implements Strategy {
        @Override
        public void doSomething() {
            System.out.println("具体策略1的运算法则...");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    @Component("concreteStrategy2")
    public class ConcreteStrategy2 implements Strategy {
        @Override
        public void doSomething() {
            System.out.println("具体策略2的运算法则...");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    @Component("defaultStrategy")
    public class DefaultStrategy implements Strategy {
        @Override
        public void doSomething() {
            System.out.println("默认策略的运算法则...");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    @Component
    public class StrategyFactory {
        //Spring会自动将Strategy接口的实现类注入到这个Map中,key为bean id,value值则为对应的策略实现类
        @Autowired
        private Map<String, Strategy> strategyMap;
    
        public Strategy getStrategy(String strategyName) {
            return strategyMap.get(strategyName);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    @SpringBootTest
    class SpringbootDemoApplicationTests {
    
        @Autowired
        private StrategyFactory strategyFactory;
    
        @Test
        public void test() {
            strategyFactory.getStrategy("concreteStrategy1").doSomething();
            strategyFactory.getStrategy("concreteStrategy2").doSomething();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    工作中遇到场景

    订阅视频平台事件(异常振动,蒸汽泄露,未带安全帽…),每个类型的事件对应一个处理类(策略类),后续可能还会订阅其他的事件.

  • 相关阅读:
    6.5 图的遍历
    AD知识总结
    机器学习与模式识别作业----决策树属性划分计算
    还未入职,这位将来的博导为学生规划了一条高效学习之路
    OSSCore 开源解决方案介绍
    windows下 PHP 安装
    2023秋招的第一个意向书
    Spring 源码(16)Spring Bean的创建过程(7)属性填充
    盲盒电商模式:融合神秘感与趣味性,带来消费者购买欲望与商业利益
    pcd和ply方式存储点云简单介绍,以及ply格式转换为pcd格式点云方法
  • 原文地址:https://blog.csdn.net/weixin_41883161/article/details/136495780