【摘要】策略模式是指有一定行动内容的相对稳定的策略名称。

前言
https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/util/Comparator.html
https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/lang/Comparable.html
Comparable
下面代码定义了对一个数组的排序。
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
   | public class Main {     public static void main(String[] args) {         int[] a = {9,2,3,5,7,1,4};         Sorter sorter = new Sorter();         sorter.sort(a);         System.out.println(Arrays.toString(a));     } }
  public class Sorter {     public static void sort(int[] 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] < arr[minPos] ? j : minPos;             }             swap(arr, i, minPos);         }     }     static void swap(int[] arr, int i, int j) {         int temp = arr[i];         arr[i] = arr[j];         arr[j] = temp;     } }
  | 
 
观察后发现,上述代码只能对int类型的数组进行排序。那么,要是想对double类型的数组进行排序,我们要怎么办?其实很简单,我们可以重载sort()方法,参数为double类型数组。就像这样sort(double[] arr)。float类型也可以继续重载sort()方法。考虑下面的情况,我们定义一个类为Cat,如何对它进行排序呢?
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 38 39 40 41 42
   | public class Cat {     int weight;     int height;
      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;     } }
  public class Sorter {     public static 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;     } }
  public class Main {     public static void main(String[] args) {         Cat[] a = {new Cat(1,2),new Cat(5,5),new Cat(3,3)};         Sorter sorter = new Sorter();         sorter.sort(a);         System.out.println(Arrays.toString(a));     } }
  | 
 
上面我们实现了对Cat的排序,如果我们想要对Dog、Car、Monkey等等一系列实体进行排序?怎么办?
所以说,我们的sort()的参数类型写死肯定是不行的。如果写成sort(Object[] objects),可能会存在有的实体类不存在compareTo()方法,没法比较大小导致也不能排序。
那么好了,我们可以要求想要排序的对象都要必须实现一个包含compareTo()方法的接口。平时使用的也就是java.lang.Comparable。下面我们自定义一下这个接口。
1 2 3 4 5
   | package com.aiz.dp.strategy;
  public interface Comparable<T> {     int compareTo(T o); }
   | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
   | public class Sorter {             public static 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;     }
  }
  | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
   | public class Dog implements Comparable<Dog>{     int food;
      public Dog(int food) {         this.food = food;     }
      @Override     public int compareTo(Dog dog) {         if (this.food < dog.food) return -1;         else if (this.food > dog.food) return 1;         else return 0;     }
      @Override     public String toString() {         return "Dog{" +                 "food=" + food +                 '}';     } }
  | 
 
1 2 3 4 5 6 7 8
   | public class Main {     public static void main(String[] args) {         Dog[] a = {new Dog(2),new Dog(3),new Dog(1)};         Sorter sorter = new Sorter();         sorter.sort(a);         System.out.println(Arrays.toString(a));     } }
  | 
 
策略模式Strategy
上面我们介绍了Comparable的使用,但是那种方式并不是策略模式。还是看上面的Cat类,我例子中是用weight进行比较大小的,如果我们想使用height比较又该怎么办呢?
Comparator