【设计模式-03】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

对任意数据类型的数组进行排序,且可以灵活的定义排序策略。

如:定义一个猫对象有高和重量两个属性,可以灵活根据猫对象的属性策略进行排序。

三、重新调整修改

1、定义支持泛型的Comparator比较器

/**
 * @description: 比较器接口
 * @author: flygo
 * @time: 2022/7/4 16:26
 */
public interface Comparator<T> {

  int compare(T o1, T o2);
}

2、调整Sorter排序类和方法

/**
 * @description: 排序对象
 * @author: flygo
 * @time: 2022/7/4 11:16
 */
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);
    }
  }

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

3、定义猫高度的比较器

实现比较器Comparator接口中的compare方法,按猫的高度进行比较

/**
 * @description: 猫按高比较
 * @author: flygo
 * @time: 2022/7/4 16:29
 */
public class CatHeightComparator implements Comparator<Cat> {

  @Override
  public int compare(Cat o1, Cat o2) {
    if (o1.getHeight() < o2.getHeight()) return -1;
    else if (o1.getHeight() > o2.getHeight()) return 1;
    else return 0;
  }
}

4、定义猫重量的比较器

/**
 * @description: 猫按体重比较
 * @author: flygo
 * @time: 2022/7/4 16:29
 */
public class CatWeightComparator implements Comparator<Cat> {

  @Override
  public int compare(Cat o1, Cat o2) {
    if (o1.getWeight() > o2.getWeight()) return -1;
    else if (o1.getWeight() < o2.getWeight()) return 1;
    else return 0;
  }
}

5、主方法实现排序

最终实现效果:可以自定猫的排序策略,对猫数组进行排序。

如排序策略:CatHeightComparatorCatWeightComparator可以灵活根据策略进行排序。

import java.util.Arrays;

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

  public static void main(String[] args) {
    Cat[] arr = {new Cat(3, 3), new Cat(5, 5), new Cat(1, 1)};

    Sorter sorter = new Sorter();

    //    CatHeightComparator comparator = new CatHeightComparator();
    CatWeightComparator comparator = new CatWeightComparator();

    sorter.sort(arr, comparator);

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

四、Lambda表达式写法

Comparator接口只有一个方法,可以定义为函数式接口。如果默认只有一个方法,@FunctionalInterface可以省略不写。

1、Comparator 接口定义为 @FunctionalInterface

/**
 * @description: 比较器接口
 * @author: flygo
 * @time: 2022/7/4 16:26
 */
@FunctionalInterface
public interface Comparator<T> {

  int compare(T o1, T o2);
}

2、Lambda表达式调用方式

import java.util.Arrays;

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

  public static void main(String[] args) {
    method2();
  }

  private static void method2() {
    Cat[] arr = {new Cat(3, 3), new Cat(5, 5), new Cat(1, 1)};

    Sorter<Cat> sorter = new Sorter();

    //    CatHeightComparator comparator = new CatHeightComparator();
    CatWeightComparator comparator = new CatWeightComparator();

    sorter.sort(
        arr,
        (o1, o2) -> {
          if (o1.getHeight() < o2.getHeight()) return -1;
          else if (o1.getHeight() > o2.getHeight()) return 1;
          else return 0;
        });

    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-13 07:36:03       50 阅读
  2. 设计模式——策略模式Strategy

    2024-01-13 07:36:03       36 阅读
  3. 设计模式--策略模式Strategy Pattern)

    2024-01-13 07:36:03       50 阅读

最近更新

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

    2024-01-13 07:36:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-13 07:36:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-13 07:36:03       82 阅读
  4. Python语言-面向对象

    2024-01-13 07:36:03       91 阅读

热门阅读

  1. 「HDLBits题解」Module shift

    2024-01-13 07:36:03       63 阅读
  2. vc++开发地图

    2024-01-13 07:36:03       69 阅读
  3. golang学习-golang结构体和Json相互转换

    2024-01-13 07:36:03       57 阅读
  4. Vue 3 Composition API 详解

    2024-01-13 07:36:03       51 阅读
  5. 【Webpack】预处理器 - loader配置介绍

    2024-01-13 07:36:03       49 阅读
  6. Go语言的垃圾回收器

    2024-01-13 07:36:03       59 阅读
  7. go 语言中 map 的相关知识

    2024-01-13 07:36:03       42 阅读
  8. Ajax 是什么? 如何创建一个 Ajax?

    2024-01-13 07:36:03       49 阅读
  9. FPGA难学在哪里,要如何学习?

    2024-01-13 07:36:03       60 阅读
  10. TCP原理

    2024-01-13 07:36:03       47 阅读
  11. 响应式编程WebFlux基础API

    2024-01-13 07:36:03       50 阅读
  12. 常见的远程过程调用(RPC)分析

    2024-01-13 07:36:03       61 阅读
  13. GPT-4:智能语言模型的新篇章

    2024-01-13 07:36:03       55 阅读
  14. 代码随想录day27 回溯继续深造,新题型

    2024-01-13 07:36:03       35 阅读