Comparable
Comparable 此接口強行對實現它的每個類的對象進行整體排序。這種排序被稱爲類的自然排序,類的 compareTo 方法被稱爲它的自然比較方法。對象本身就已經支持自比較所需要實現的接口(如 String、Integer 自己就可以完成比較大小操作)。
Comparator
如果一個類不能實現Comparable接口,那麼我們自己可以提供Comparator的排序,如果你不喜歡缺省的Comparator行爲,照樣可以編寫自己的Comparator。
策略模式:策略模式定義了一系列的算法,並將每一個算法封裝起來,而且使它們還可以相互替換。策略模式讓算法獨立於使用它的客戶而獨立變化。
之所以要談到策略模式,就是因爲Comparator接口其實就是一種策略模式的實踐。實現Comparator接口的類必然就會實現一個compare(Object o1, Object o2)的方法,而這個方法就是算法中的一部分,所有使用了compare方法的類都不會關心compare是如何工作的,只關心他的返回值,這也是面向對象中著名的封裝特性。
下面模擬java中comparable和comparator的實現:
首先編寫Comparable接口:
package com.lcq.strategy;
/**
* 創建比較接口
* @author lcq
*
*/
public interface Comparable {
public int compareTo(Object o);
}
然後編寫Comparator接口:
package com.lcq.strategy;
public interface Comparator {
int compare(Object o1, Object o2);
}
編寫排序類DataSorter:
package com.lcq.strategy;
public class DataSorter {
/**
* 編寫冒泡排序方法
*
* @param a
*/
public static void sort(int[] a) {
for (int i = a.length; i > 0; i--) {
for (int j = 0; j < i - 1; j++) {
if (a[j] > a[j + 1]) {
swap(a, j, j + 1);
}
}
}
}
/**
* 交換兩個數的值
*
* @param a
* @param x
* @param y
*/
private static void swap(int[] a, int x, int y) {
int temp;
temp = a[x];
a[x] = a[y];
a[y] = temp;
}
/**
* 打印出數組的值
*
* @param a
*/
public static void print(int[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
/**
* 打印出數組的值
*
* @param a
*/
public static void print(Object[] a) {
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
/**
* 編寫可以對任意對象進行排序的方法
* @param a
*/
public static void 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);
}
}
}
}
/**
* 交換任意對象
* @param a
* @param x
* @param y
*/
private static void swap(Object[] a, int x, int y) {
Object temp;
temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}
爲了測試,我們編寫Cat類,在Cat類中繼承Comparable接口重寫compareTo方法:
package com.lcq.strategy;
/**
* 創建測試的工具類,並且繼承Comparable接口,實現compareTo方法
*
* @author lcq
*
*/
public class Cat implements Comparable {
private int height;
private int weight;
private Comparator comparator = new CatHeightComparator();
public Comparator getComparator() {
return comparator;
}
public void setComparator(Comparator comparator) {
this.comparator = comparator;
}
public Cat(int height, int weight) {
super();
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;
}
@Override
public String toString() {
return this.getHeight() + "|" + this.getWeight();
}
@Override
public int compareTo(Object o) {
// if (o instanceof Cat) {
// Cat c = (Cat) o;
// if (this.getHeight() > c.getHeight())
// return 1;
// else if (this.getHeight() < c.getHeight())
// return -1;
// else
// return 0;
// }
// return -100;
return this.comparator.compare(this, o);
}
}
編寫Cat類的比較器類CatHeightComparator:
package com.lcq.strategy;
public class CatHeightComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
Cat c1 = (Cat)o1;
Cat c2 = (Cat)o2;
if(c1.getHeight() > c2.getHeight()) return 1;
else if(c1.getHeight() < c2.getHeight()) return -1;
return 0;
}
}
最後是測試類Test:
package com.lcq.strategy;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// int[] a = {4,5,9,2,3,7};
Cat[] a = {new Cat(1,1),new Cat(4,4),new Cat(2,2)};
DataSorter.sort(a);
DataSorter.print(a);
}
}
現在想實現Cat的反序排列,只需要再實現一個Comparator接口,反序比較Cat即可。
參考資料:
http://blog.csdn.net/lyjiau/article/details/17043285
http://blog.csdn.net/liuchangqing123/article/details/7345588