【设计模式-02】Strategy策略模式及应用场景

一、参考资料

  • Java 官方文档

Overview (Java SE 18 & JDK 18)module indexicon-default.png?t=N7T8https://docs.oracle.com/en/java/javase/18/docs/api/index.html

  • Java中使用到的策略模式

Comparator、comparable

Comparator (Java SE 18 & JDK 18)declaration: module: java.base, package: java.util, interface: Comparatoricon-default.png?t=N7T8https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/util/Comparator.html

Comparable (Java SE 18 & JDK 18)declaration: module: java.base, package: java.lang, interface: Comparableicon-default.png?t=N7T8https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/Comparable.html

二、主要设计思想

对更改关闭,对扩展开放,程序更有弹性,可扩展性强。

Extensibility、Scalability

三、应用场景

对任意数据类型的数组进行排序,如对int、double、float以及对象的排序。

1、Cat.java 对象

/**
 * @description: 猫对象
 * @author: flygo
 * @time: 2022/7/4 11:11
 */
public class Cat {

  private int height, weight;

  public Cat() {}

  public Cat(int weight, int height) {
    this.weight = weight;
    this.height = height;
  }

  public int compareTo(Cat c) {
    if (this.weight < c.weight) return -1;
    else if (this.weight > c.weight) return 1;
    else return 0;
  }

  @Override
  public String toString() {
    return "Cat{" + "height=" + height + ", weight=" + weight + '}';
  }
}

2、Sorter.java 排序类

/**
 * @description: 排序对象
 * @author: flygo
 * @time: 2022/7/4 11:16
 */
public class Sorter {

  public void sort(Cat[] arr) {
    for (int i = 0; i < arr.length - 1; i++) {
      int minPos = i;
      for (int j = i + 1; j < arr.length; j++) {
        minPos = arr[j].compareTo(arr[minPos]) == -1 ? j : minPos;
      }
      swap(arr, i, minPos);
    }
  }

  static void swap(Cat[] arr, int i, int j) {
    Cat temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
  }
}

3、StrategyMain 主类

import java.util.Arrays;

/**
 * @description: 主方法
 * @author: flygo
 * @time: 2022/7/4 11:15
 */
public class StrategyMain {

  public static void main(String[] args) {
    //    int[] arr = {2, 4, 3, 6, 10, 1};

    Cat[] arr = {new Cat(3, 3), new Cat(5, 5), new Cat(1, 1)};
    Sorter sorter = new Sorter();
    sorter.sort(arr);

    System.out.println(Arrays.toString(arr));
  }
}

四、使用泛型调整修改

1、定义Comparable接口

注意接口中使用了泛型 <T>,定义了compareTo接口方法。

/**
 * @description: 比较接口类
 * @author: flygo
 * @time: 2022/7/4 15:46
 */
public interface Comparable<T> {

  int compareTo(T o);
}

2、Sorter 排序类

使用Comparable 接口类承接对象数组,只要实现了Comparable接口中compareTo方法,都可以进行排序。

/**
 * @description: 排序对象
 * @author: flygo
 * @time: 2022/7/4 11:16
 */
public class Sorter {

  public void sort(Comparable[] arr) {
    for (int i = 0; i < arr.length - 1; i++) {
      int minPos = i;
      for (int j = i + 1; j < arr.length; j++) {
        minPos = arr[j].compareTo(arr[minPos]) == -1 ? j : minPos;
      }
      swap(arr, i, minPos);
    }
  }

  static void swap(Comparable[] arr, int i, int j) {
    Comparable temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
  }
}

3、定义一个需要排序的对象,如Dog

Dog对象实现 Comparable 接口类中的 compareTo 方法

/**
 * @description: 狗
 * @author: flygo
 * @time: 2022/7/4 15:48
 */
public class Dog implements Comparable<Dog> {

  int food;

  public Dog(int food) {
    this.food = food;
  }

  @Override
  public int compareTo(Dog o) {
    if (this.food < o.food) return -1;
    else if (this.food > o.food) return 1;
    else return 0;
  }

  @Override
  public String toString() {
    return "Dog{" + "food=" + food + '}';
  }
}

4、最终效果

最终可以实现对实现Comparable 接口compareTo方法的任意对象进行排序

import java.util.Arrays;

/**
 * @description: 主方法
 * @author: flygo
 * @time: 2022/7/4 11:15
 */
public class StrategyMain {

  public static void main(String[] args) {
    //    int[] arr = {2, 4, 3, 6, 10, 1};
    //    Cat[] arr = {new Cat(3, 3), new Cat(5, 5), new Cat(1, 1)};

    Dog[] arr = {new Dog(5), new Dog(1), new Dog(3)};

    Sorter sorter = new Sorter();
    sorter.sort(arr);

    System.out.println(Arrays.toString(arr));
  }
}

五、源码地址

GitHub - jxaufang168/Design-Patterns: 设计模式学习设计模式学习. Contribute to jxaufang168/Design-Patterns development by creating an account on GitHub.icon-default.png?t=N7T8https://github.com/jxaufang168/Design-Patterns



 

相关推荐

  1. 设计模式-策略模式 Strategy

    2024-01-10 20:40:02       50 阅读
  2. 设计模式——策略模式Strategy

    2024-01-10 20:40:02       36 阅读
  3. 设计模式--策略模式Strategy Pattern)

    2024-01-10 20:40:02       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-10 20:40:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-10 20:40:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-10 20:40:02       82 阅读
  4. Python语言-面向对象

    2024-01-10 20:40:02       91 阅读

热门阅读

  1. 面试经典题---68.文本左右对齐

    2024-01-10 20:40:02       54 阅读
  2. 鸿蒙开发已解决-ArkTS@State 数组无法触发重绘

    2024-01-10 20:40:02       59 阅读
  3. Webservice,WCF,WebAPI--特点及进化史

    2024-01-10 20:40:02       51 阅读
  4. Qt - QML与C++数据交互详解

    2024-01-10 20:40:02       48 阅读
  5. Vue项目路由路径中去掉#

    2024-01-10 20:40:02       57 阅读
  6. DAY 10 | 1047, (20,150)

    2024-01-10 20:40:02       58 阅读
  7. Edge扩展插件如何安装位置?

    2024-01-10 20:40:02       56 阅读
  8. 关于frida定位无误却无法hook到加密方法的见解

    2024-01-10 20:40:02       63 阅读