策略模式:(strategy)定义算法家族,分别封装起来,让他们之间可以相互替换。此模式可以让算法的变化,不影响使用算法的用户。
详细介绍:https://blog.youkuaiyun.com/smartboy_01/article/details/42271885
使用策略模式构建的通用排序步骤
- 定义DataSort排序类
public class DataSorter {
/**
* 冒泡排序方法
* @param a
*/
public static Object sort(Object[] a) {
for(int i = a.length; i > 0; i--) {
for(int j = 0; j < i-1; j++) {
Comparable o1 = (Comparable)a[j];
Comparable o2 = (Comparable)a[j+1];
if(o1.compareTo(o2) == 1) {
swap(a, j , j+1);
}
}
}
return a;
}
/**
* 交换
* @param a
* @param x
* @param y
*/
private static void swap(Object[] a, int x, int y) {
Object temp = a[x];
a[x] = a[y];
a[y] = temp;
}
/**
* 输出排序结果
* @param objects
*/
public static void printSortResult(Object[] objects){
for (Object object : objects) {
System.out.print(object + " ");
}
System.out.println();
}
}
- 定义
Comparable
接口,包含int compareTo(Object obj)
方法,正如Java JDK自带的Comparable类,所有要排序的类,都实现此接口,实现各自的compareTo()方法,这样就DataSort类就可以实现了复用。(此时并未使用到策略模式)
public interface Comparable {
/**
* 比较方法
* @param o
* @return
*/
int compareTo(Object o);
}
- 创建要排序的类,实现
Comparable
接口,实现compareTo()
方法,此时每次排序条件改变都要修改compareTo()
方法中的排序条件,比较繁琐,不利于拓展。
public class Tiger implements Comparable {
//老虎重量
private int weight;
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public Tiger(int weight) {
this.weight = weight;
}
/**
* 自定义排序方法
*/
@Override
public int compareTo(Object o) {
int rs = 0;
Tiger tiger = (Tiger) o;
if(this.weight > tiger.getWeight()){
rs = 1;
}else if(this.weight < tiger.getWeight()){
rs = -1;
}else{
rs = 0;
}
return rs;
}
@Override
public String toString() {
return this.weight + "";
}
}
但是,有时我们有很多的排序规则,事先并不确定,这是修改compareTo()
方法将是一个很不明智的选择。这时,可以抽象出公共算法接口,如:Comparator
,而它的子类实现具体的排序规则,当需要更多的排序规则时,仅仅需要创建新的类实现Comparator
就可以。这样就使我们的程序拥有更好的可扩展性。
- 抽象出公共算法接口
Comparator
/**
* 抽象出来比较方法
* 使用策略模式
*/
@SuppressWarnings("ALL")
public interface Comparator {
int compare(Object o1, Object o2);
}
- 创建需要排序的类,实现
Comparable
接口
public class Dog implements Comparable{
private int height;
private int weight;
//设置不同的比较类,进行不同条件比较
private Comparator comparator = new DogHeightComparator();
public Dog(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;
}
public Comparator getComparator() {
return comparator;
}
public void setComparator(Comparator comparator) {
this.comparator = comparator;
}
public String toString() {
return height + "|" + weight;
}
@Override
public int compareTo(Object o) {
return comparator.compare(this,o);
}
}
- 创建具有具体的排序规则比较器类
/**
* 高度比较器
*/
@SuppressWarnings("ALL")
public class DogHeightComparator implements Comparator{
@Override
public int compare(Object o1, Object o2) {
Dog dog1 = (Dog) o1;
Dog dog2 = (Dog) o2;
int rs = 0;
if(dog1.getHeight() > dog2.getHeight()){
rs = 1;
}else if(dog1.getHeight() < dog2.getHeight()){
rs = -1;
}else {
rs = 0;
}
return rs;
}
}
/**
* 重量比较器
*/
public class DogWeightComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Dog dog1 = (Dog) o1;
Dog dog2 = (Dog) o2;
int rs = 0;
if(dog1.getWeight() > dog2.getWeight()){
rs = 1;
}else if(dog1.getWeight() < dog2.getWeight()){
rs = -1;
}else {
rs = 0;
}
return rs;
}
}
Comparator
的子类实现具体的排序规则,当需要更多的排序规则时,仅仅需要创建新的类实现Comparator
就可以。这样就使我们的程序拥有更好的可扩展性。
类图如下: