• 设计模式 | 策略模式


    1 前言

    策略模式(Strategy Pattern) 中,一个类的行为或其算法可以在运行时更改。这种类型的设计模式属于行为型模式。

    策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 context 对象。策略对象改变 context 对象的执行算法。

    典型应用场景就是比较器,本文将实现一个通用的比较器


    2 比较器的实现

    这是策略器-比较器的包结构,具体代码以及描述在下方
    在这里插入图片描述

    场景:我们要实现一个可以适用于不同场景的比较器,比如比较猫的大小(分别可以使用身高、体重做比较)、比较狗的大小(分别使用饭量、身高做比较)…等等


    2.1 猫狗po定义

    Cat.java

    package com.chen.design_pattern.strategy.po;
    
    public class Cat {
        public int height;
        public int weight;
    
        @Override
        public String toString() {
            return "Cat{" +
                    "height=" + height +
                    ", weight=" + weight +
                    '}';
        }
    
        public Cat(int height, int weight) {
            this.height = height;
            this.weight = weight;
        }
    
        public int getHeight() {
            return height;
        }
    
        public void setHeight(int height) {
            this.height = height;
        }
    
        public int getWeight() {
            return weight;
        }
    
        public void setWeight(int weight) {
            this.weight = weight;
        }
    }
    
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36

    Dog.java

    package com.chen.design_pattern.strategy.po;
    
    public class Dog {
        public int height;
        public int weight;
    
        public Dog(int height, int weight) {
            this.height = height;
            this.weight = weight;
        }
    
        @Override
        public String toString() {
            return "Dog{" +
                    "height=" + height +
                    ", weight=" + weight +
                    '}';
        }
    
        public int getHeight() {
            return height;
        }
    
        public void setHeight(int height) {
            this.height = height;
        }
    
        public int getWeight() {
            return weight;
        }
    
        public void setWeight(int weight) {
            this.weight = weight;
        }
    }
    
    
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    2.2 比较器定义

    先生成一个通用接口
    Comparator.java

    package com.chen.design_pattern.strategy.comparator;
    
    public interface Comparator<T> {
    
        int compare(T o1, T o2);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    对接口进行不同的实现:
    猫身高比较器:CatHeightComparator.java

    package com.chen.design_pattern.strategy.comparator;
    
    import com.chen.design_pattern.strategy.po.Cat;
    
    public class CatHeightComparator implements Comparator<Cat> {
    
        @Override
        public int compare(Cat o1, Cat o2) {
            if (o1.height > o2.height) return 1;
            else if (o1.height < o2.height) return -1;
            return 0;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    猫体重比较器:CatWeightComparator.java

    package com.chen.design_pattern.strategy.comparator;
    
    import com.chen.design_pattern.strategy.po.Cat;
    
    public class CatWeightComparator implements Comparator<Cat> {
        @Override
        public int compare(Cat o1, Cat o2) {
            if (o1.weight > o2.weight) return 1;
            else if (o1.weight < o2.weight) return -1;
            return 0;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.3 比较并交换的实现(交换排序算法器)

    Sorter.java

    package com.chen.design_pattern.strategy.sorter;
    
    import com.chen.design_pattern.strategy.comparator.Comparator;
    
    public class Sorter<T> {
    
        public void sort(T[] arr, Comparator<T> comparator) {
            for (int i = 0; i < arr.length - 1; i++) {
                int minPos = i;
                for (int j = i + 1; j < arr.length; j++) {
                    minPos = comparator.compare(arr[j], arr[minPos]) == -1 ? j : minPos;
                }
                swap(arr, i, minPos);
            }
        }
    
        private void swap(T[] arr, int i, int minPos) {
            T temp = arr[i];
            arr[i] = arr[minPos];
            arr[minPos] = temp;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    2.4 定义Main方法

    Main.java

    package com.chen.design_pattern.strategy;
    
    import com.chen.design_pattern.strategy.comparator.CatHeightComparator;
    import com.chen.design_pattern.strategy.comparator.CatWeightComparator;
    import com.chen.design_pattern.strategy.po.Cat;
    import com.chen.design_pattern.strategy.sorter.Sorter;
    
    import java.util.Arrays;
    
    public class Main {
    
        public static void main(String[] args) {
    
            Cat[] cats = {
                    new Cat(12, 50),
                    new Cat(222, 30),
                    new Cat(3, 40)
            };
    
            Sorter<Cat> sorter = new Sorter<>();
            // 按照猫的身高排序
            sorter.sort(cats, new CatHeightComparator());
            System.out.println("身高排序: " + Arrays.toString(cats));
            // 按猫的体重排序
            sorter.sort(cats, new CatWeightComparator());
            System.out.println("体重排序: " + Arrays.toString(cats));
        }
    }
    
    
    • 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

    3 运行结果验证

    只需要new一个Sorter() 并指定比较类型的为泛型,传入对应的数组和比较器规则(Comparator) 即可完成同一实体类之间不同属性的比较以及排序

    在这里插入图片描述

  • 相关阅读:
    VSCode修改主题为Eclipse 绿色护眼模式
    CSS画圆以及CSS实现动态圆
    今日睡眠质量记录79分
    使用 Windows Core Audio APIs 进行 Loopback Recording 并生成 WAV 文件
    【OpenGauss源码学习 —— 执行算子(Append算子)】
    666666666666666
    (一). 贝叶斯滤波器
    一种解决问题E: Unable to locate package python-vcstool的方法
    Kruskal,最短路综合应用,一道图论一
    [二叉树&单调栈/递归] LeetCode 654. 最大二叉树(笛卡尔树)
  • 原文地址:https://blog.csdn.net/weixin_40597409/article/details/126686384