C&C當中的策略(strategy)模式
設計模式的就是把簡答的問題複雜化,原因是爲了擴展。產生多態,抽象,複雜化。
本文word格式下載:
http://wenku.baidu.com/view/40ef9919cc7931b765ce159b.html?st=1
C&C就是指Comparable和Comparator接口。
1.Comparable接口
爲了比較兩個對象的大小,爲了能對對象進行排序。在java當中定義了comparable接口:
注意comparable是在java.lang當中。
package com.bjsxt.dp.strategy;
publicinterface Comparable {
//和某個目標(對象)進行比較,注意返回值是int類型,
//如果返回值是>0的,就是大於。
//如果返回值是小於<0的,就是小於。
//如果返回值是=0的,就是等於。
publicint compareTo(Object o);
}
Cat類:
//在cat這個類當中,具體的比較方法的實現
publicint compareTo(Cat o) {
if (oinstanceof Cat){
Cat c = (Cat) o;
if(this.getHeight() > c.getHeight())
return 1;
elseif (this.getHeight() < c.getHeight())
return -1;
elsereturn 0;
}
//如果傳過來的對象不是cat的instance,應該拋出異常
else
return -100;
}
DataSort類:
在DataSort當中進行排序的算法:
//對object進行排序的算法
publicstaticvoid sort(Object[] a) {
for(int i=a.length; i>0; i--) {
for(int j=0; j<i-1; j++) {
Comparableo1 = (Comparable)a[j];
Comparableo2 = (Comparable)a[j+1];
if(o1.compareTo(o2) == 1) {
swap(a, j , j+1);
}
}
}
}
//對object進行交換的算法
privatestaticvoid swap(Object[] a,int x,int y) {
Object temp = a[x];
a[x] = a[y];
a[y] = temp;
}
Dog類:
package com.bjsxt.dp.strategy;
publicclass Dogimplements Comparable {
public Dog(int food) {
super();
this.food = food;
}
privateintfood;
publicint getFood() {
returnfood;
}
publicvoid setFood(int food) {
this.food = food;
}
//@Override
//對dog進行排序,具體的實現在dog類當中,datasort方法不用改。
publicint compareTo(Object o) {
Dog d = (Dog)o;
if(this.food > d.getFood()) return 1;
elseif(this.food < d.getFood()) return -1;
elsereturn 0;
}
@Override
public String toString() {
returnthis.food +"";
}
}
要注意的是,目前爲止,我們都只能採用一種邏輯對數據排序。比如對Cat只能對height,對Dog只能對food。
2. Comparator接口
package com.bjsxt.dp.strategy;
//大小比較器,也就是說是比較策略
publicinterface Comparator {
//比較兩個對象的大小
//o1>o2返回正數
//o1<o2返回負數
//o1=o2返回0
int compare(Object o1, Object o2);
}
package com.bjsxt.dp.strategy;
publicclass Catimplements java.lang.Comparable<Cat> {
privateintheight;
//private Comparator comparator = newCatHeightComparator();
//定義了一個成員變量comparator,根據不同的設定來對應不同的比較策略。如果想修改比較策略,只需要修改這個comparator去指向不同的子類對象。
private java.util.Comparator<Cat>comparator =new CatHeightComparator();
private Comparatorcomparator1 =new CatWeightComparator();
public java.util.Comparator getComparator() {
returncomparator;
}
publicvoid setComparator(java.util.Comparator comparator) {
this.comparator = comparator;
}
//這裏的comparator實現的是HeightComparator。
publicint compareTo(Cat o) {
returncomparator.compare(this, o);
}
publicint getHeight() {
returnheight;
}
publicvoid setHeight(int height) {
this.height = height;
}
public Cat(int height,int weight) {
super();
this.height = height;
this.weight = weight;
}
publicint getWeight() {
returnweight;
}
publicvoid setWeight(int weight) {
this.weight = weight;
}
privateintweight;
@Override
public String toString() {
returnheight +"|" +weight;
}
}
思想:
1. 兩個接口都是必要的。comparable接口是爲了能對象比較大小,也就是說在DataSort當中,有個假設,就是要比較的對象都是實現了comparable接口的。
2. comparator接口是爲了能實現多個比較的邏輯,從而使用不同的比較策略。所以嚴格來說,在使用了comparator接口,纔是真的比較策略。
3. 個人感覺,所謂的策略模式,就是定義多個不同的策略,都去實現comparator接口,成爲不同的策略選擇器。在具體的使用的時候,使用者可以靈活選擇不同的策略。
4. 那麼也就是說,預先寫好不同的多個策略,根據具體的情況來選擇使用哪個?注意,這裏是面向對象的思維,採取策略模式。事實上,如果我們用傳統的面向過程的思維,這裏就是一個switch語句。多個不同的策略已經寫好,並放在那邊,通過switch選擇使用哪個或者哪幾個策略。