设计模式系列--Strategy
2019獨角獸企業重金招聘Python工程師標準>>>
一.類圖
?
二.意圖
定義一系列的算法,把它們一個個封裝起來,?并且使它們可相互替換。本模式使得算法可獨立于使用它的客戶而變化。
三.適用性
a)?許多相關的類僅僅是行為有異。“策略”提供了一種用多個行為中的一個行為來配置一個類的方法。
b)?需要使用一個算法的不同變體。例如,你可能會定義一些反映不同的空間/時間權衡的算法。當這些變體實現為一個算法的類層次時[?H?O?8?7?]?,可以使用策略模式。
c)?算法使用客戶不應該知道的數據??墒褂貌呗阅J揭员苊獗┞稄碗s的、與算法相關的數據結構。
d)?一個類定義了多種行為,?并且這些行為在這個類的操作中以多個條件語句的形式出現。將相關的條件分支移入它們各自的S?t?r?a?t?e?g?y?類中以代替這些條件語句。
四.實例
諸葛亮的錦囊妙計:
第一條:讓趙云一行先去訪問喬國老。喬國老是周瑜和孫策的丈人,在東吳有一定的威望,而且和吳國太關系不錯。訪喬國老一來可以把孫劉聯姻之事告訴吳國太(吳國太此前并不知道),讓孫權周瑜陷于被動。二來可以讓他在吳國太面前美言,增加孫劉聯姻的可能性。
第二條:騙說曹操為報赤壁之仇,舉兵南下,讓劉備速回荊州。劉備在東吳整天花天酒地,樂不思蜀,這是周瑜設計想讓劉備玩物喪志。
第三條:用孫夫人來擋追兵。孫尚香好歹也是東吳郡主,她的話,東吳的將領還是得掂量掂量的。
package explore.strategy;public interface Strategy {
public void perform();
}
package explore.strategy;
public class Context {
private Strategy strategy;
public Context(Strategy strategy) {
this.strategy = strategy;
}
public void setStrategy(Strategy s) {
this.strategy = s;
}
public void perform() {
this.strategy.perform();
}
}
package explore.strategy;
public class TheFirstStrategy implements Strategy {
@Override
public void perform() {
System.out.println("讓趙云一行先去訪問喬國老。");
}
}
package explore.strategy;
public class TheSecondStrategy implements Strategy {
@Override
public void perform() {
System.out.println("騙說曹操為報赤壁之仇,舉兵南下,讓劉備速回荊州。");
}
}
package explore.strategy;
public class TheThirdStrategy implements Strategy {
@Override
public void perform() {
System.out.println("用孫夫人來擋追兵。");
}
}
package explore.strategy;
public class ZhaoYun {
public static void main(String[] args) {
//第一條:讓趙云一行先去訪問喬國老。
Strategy first = new TheFirstStrategy();
//第二條:騙說曹操為報赤壁之仇,舉兵南下,讓劉備速回荊州。
Strategy second = new TheSecondStrategy();
//第三條:用孫夫人來擋追兵。
Strategy third = new TheThirdStrategy();
Context c = new Context(first);
c.perform();
c.setStrategy(second);
c.perform();
c.setStrategy(third);
c.perform();
}
}
五.Strategy模式在比較器(Comparator)里的應用
?
//比較器public interface Comparator<T> {
int compare(T o1, T o2);
... ...
//自定義比較算法
public interface Comparable<T> {
public int compareTo(T o);
//Integer自定義的比較算法
public final class Integer extends Number implements Comparable<Integer> {
public int compareTo(Integer anotherInteger) {
int thisVal = this.value;
int anotherVal = anotherInteger.value;
return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
}
... ...
//String類中定義的比較算法
public int compareTo(String anotherString) {
int len1 = count;
int len2 = anotherString.count;
int n = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
if (i == j) {
int k = i;
int lim = n + i;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
} else {
while (n-- != 0) {
char c1 = v1[i++];
char c2 = v2[j++];
if (c1 != c2) {
return c1 - c2;
}
}
}
return len1 - len2;
}
?
當我們比較兩個對象的時候只需調用一個compare()方法就可以了:
int cmp = comparator.compare(obj1, obj2);轉載于:https://my.oschina.net/u/197668/blog/361207
總結
以上是生活随笔為你收集整理的设计模式系列--Strategy的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于Eclipse中的开源框架EMF(E
- 下一篇: 【转】15个最受欢迎的Python开源框