• 设计模式-Strategy模式(策略模式)


    策略模式

    Stragtegy模式可以整体地替换算法的实现部分,使得用不同的算法去解决同一个问题。

    代码实现

    书上猜拳的例子看不太明白,用个简单的排序算法来试试。

    策略接口

    public interface Strategy {
        public void sort(int[] array);
    }
    
    • 1
    • 2
    • 3

    具体的策略,分别是冒泡排序和选择排序

    public class BubblingStrategy implements Strategy{
    
        @Override
        public void sort(int[] array) {
            int len = array.length;
            if (len == 0) return;
    
            for (int i = 0; i < len-1; i++) {
                for (int i1 = 0; i1 < len-1-i; i1++) {
                    if (array[i1] > array[i1+1]) {
                        int tmp = array[i1];
                        array[i1] = array[i1+1];
                        array[i1+1] = tmp;
                    }
                }
            }
            System.out.print("使用冒泡排序:");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    public class SelectStrategy implements Strategy{
        @Override
        public void sort(int[] array) {
            int len = array.length;
            if (len == 0) return;
    
            int k;
            for (int i = 0; i < len-1; i++) {
                k = i;
    
                for (int i1 = i; i1 < len; i1++) {
                    if (array[k] > array[i1]) {
                        k = i1;
                    }
                }
    
                int tmp = array[i];
                array[i] = array[k];
                array[k] = tmp;
            }
    
            System.out.print("使用选择排序:");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    上下文Context,负责使用Strategy角色,保存了具体策略的实例,委派给某个算法实现。

    public class Context {
    
        private Strategy strategy;
    
        public Context(Strategy strategy) {
            this.strategy = strategy;
        }
    
        public void sort(int[] array) {
            strategy.sort(array);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    测试类

    public class Test {
        public static void main(String[] args) {
    
            int[] array = {2,1,4,3,9,8,5,0,6,10,7};
    
            // 使用策略:选择排序
            Context context1 = new Context(new SelectStrategy());
            context1.sort(array);
            printArray(array);
    
            // 使用策略:冒泡排序
            Context context2 = new Context(new BubblingStrategy());
            context2.sort(array);
            printArray(array);
    
        }
    
        public static void printArray(int[] array) {
            for (int i = 0; i < array.length; i++) {
                System.out.print(array[i] + " ");
            }
            System.out.println("");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    总结

    通过“委托”的方式,将算法实现交给具体的策略类。感觉跟Builder模式也有点像,通过Director委派给某个Builder实现。

    Strategy模式特意将算法与其他部分分离开来,之定义了算法相关的接口,然后在程序中以委托的方式来使用算法。使用委托这种弱关联关系可以很方便地整体替换算法。

    使用场景

    1. 想通过改善算法来提高算法的处理速度。
    2. 通过切换算法来验证正确性

    在这里插入图片描述

  • 相关阅读:
    前端例程20220728:按钮点击涟漪效果
    【技巧】Windows 11 如何安装日文语言包和日文系统
    SpringBoot实现登录拦截器
    linux环境部署
    时代风口中的Web3.0基建平台,重新定义Web3.0!
    python图片压缩/格式转换
    【操作系统】错题库
    论文浅尝 | KR-GCN: 知识感知推理的可解释推荐系统
    树【LeetCode】
    TheRouter 框架原理
  • 原文地址:https://blog.csdn.net/weixin_44030848/article/details/126166907