基于比较器的策略模式
优点:使用策略模式可以加强工程扩展性,维护性。避免过多的if...else..或者switch,隐藏了策略实现的细节。
缺点:相对于if ..else 不能直观看到所有策略方式,并且每增加一种策略都要新加一个策略类。
通过以下比较猫狗大小的例子,来了解策略模式。
代码结构
实体类dto
public class Cat {
public int weight;
public int height;
public String name;
public Cat(int weight, int height, String name) {
this.weight = weight;
this.height = height;
this.name = name;
}
@Override
public String toString() {
return "Cat{" +
"weight=" + weight +
", height=" + height +
", name=" + name +
'}';
}
}
public class Dog {
public int weight;
public int height;
public String name;
public Dog(int weight, int height, String name) {
this.weight = weight;
this.height = height;
this.name = name;
}
@Override
public String toString() {
return "Dog{" +
"weight=" + weight +
", height=" + height +
", name='" + name + '\'' +
'}';
}
}
比较接口:用于不同策略实现比较方法
public interface Comparator<T> {
int copmare(T o1, T o2);
}
不同维度比较猫狗大小的策略
1、根据猫的高比较大小
public class CatHighCompare implements Comparator<Cat> {
@Override
public int copmare(Cat o1, Cat o2) {
if(o1.height > o2.height) return -1;
else if (o1.height < o2.height) return 1;
else return 0;
}
}
2、根据猫的体重比较大小
public class CatWeightCompare implements Comparator<Cat> {
@Override
public int copmare(Cat o1, Cat o2) {
if(o1.weight > o2.weight) return -1;
else if (o1.weight < o2.weight) return 1;
else return 0;
}
}
3、根据狗的高比较大小
public class DogHighCompare implements Comparator<Dog> {
@Override
public int copmare(Dog o1, Dog o2) {
if(o1.height > o2.height) return -1;
else if (o1.height < o2.height) return 1;
else return 0;
}
}
4、根据狗的体重比较大小
public class DogWeightCompare implements Comparator<Dog> {
@Override
public int copmare(Dog o1, Dog o2) {
if(o1.weight > o2.weight) return -1;
else if (o1.weight < o2.weight) return 1;
else return 0;
}
}
当然如果需要其他维度,可以继续加策略类就行
排序Sorter类
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++) {
int copmare = comparator.copmare(arr[j], arr[minPos]);
minPos = copmare == -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;
}
}
测试类
public class Test {
public static void main(String[] args) {
Cat[] cats = {new Cat(5,1,"汤姆猫"),new Cat(2,2,"吉利猫"),new Cat(6,3,"熊仔猫")};
Sorter<Cat> catSorter = new Sorter<>();//猫类型的sort
catSorter.sort(cats, new CatWeightCompare());//调用sort使用不同的比较策略
for (int i = 0; i < cats.length; i++) {
System.out.println("按照猫体重排序:"+ cats[i]);
}
System.out.println();
catSorter.sort(cats, new CatHighCompare());//调用sort使用不同的比较策略
for (int i = 0; i < cats.length; i++) {
System.out.println("按照猫身高排序:"+ cats[i]);
}
System.out.println();
Sorter<Dog> dogSorter = new Sorter<>();//狗类型的sort
Dog[] dogs = {new Dog(10,12,"汤姆狗"),new Dog(8,6,"吉利狗"),new Dog(16,22,"熊仔狗")};
dogSorter.sort(dogs, new DogWeightCompare());//调用sort使用不同的比较策略
for (int i = 0; i < cats.length; i++) {
System.out.println("按照狗身高排序:"+ cats[i]);
}
System.out.println();
dogSorter.sort(dogs, new DogHighCompare());//调用sort使用不同的比较策略
for (int i = 0; i < cats.length; i++) {
System.out.println("按照狗体重排序:"+ cats[i]);
}
}
}