日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

设计模式系列--Strategy

發(fā)布時間:2024/4/15 asp.net 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 设计模式系列--Strategy 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

一.類圖

?

二.意圖

定義一系列的算法,把它們一個個封裝起來,?并且使它們可相互替換。本模式使得算法可獨立于使用它的客戶而變化。

三.適用性

a)?許多相關(guān)的類僅僅是行為有異。策略提供了一種用多個行為中的一個行為來配置一個類的方法。

b)?需要使用一個算法的不同變體。例如,你可能會定義一些反映不同的空間/時間權(quán)衡的算法。當這些變體實現(xiàn)為一個算法的類層次時[?H?O?8?7?]?,可以使用策略模式。

c)?算法使用客戶不應該知道的數(shù)據(jù)。可使用策略模式以避免暴露復雜的、與算法相關(guān)的數(shù)據(jù)結(jié)構(gòu)。

d)?一個類定義了多種行為,?并且這些行為在這個類的操作中以多個條件語句的形式出現(xiàn)。將相關(guān)的條件分支移入它們各自的S?t?r?a?t?e?g?y?類中以代替這些條件語句。

四.實例

諸葛亮的錦囊妙計:

第一條:讓趙云一行先去訪問喬國老。喬國老是周瑜和孫策的丈人,在東吳有一定的威望,而且和吳國太關(guān)系不錯。訪喬國老一來可以把孫劉聯(lián)姻之事告訴吳國太(吳國太此前并不知道),讓孫權(quán)周瑜陷于被動。二來可以讓他在吳國太面前美言,增加孫劉聯(lián)姻的可能性。

第二條:騙說曹操為報赤壁之仇,舉兵南下,讓劉備速回荊州。劉備在東吳整天花天酒地,樂不思蜀,這是周瑜設(shè)計想讓劉備玩物喪志。

第三條:用孫夫人來擋追兵。孫尚香好歹也是東吳郡主,她的話,東吳的將領(lǐng)還是得掂量掂量的。

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;
}

?

當我們比較兩個對象的時候只需調(diào)用一個compare()方法就可以了:

int cmp = comparator.compare(obj1, obj2);







轉(zhuǎn)載于:https://my.oschina.net/u/197668/blog/361207

總結(jié)

以上是生活随笔為你收集整理的设计模式系列--Strategy的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。