.NET 下運(yùn)用策略模式(組合行為和實(shí)體的一種模式)
更新時間:2012年12月30日 12:39:05 作者:
我簡單的理解策略模式就是把行為(方法)單獨(dú)的抽象出來,并采用組合(Has-a)的方式,來組合行為和實(shí)體的一種模式比如,.NET中對數(shù)組排序的Sort的方法就是一個策略模式的實(shí)現(xiàn)模板
我簡單的理解策略模式就是把行為(方法)單獨(dú)的抽象出來,并采用組合(Has-a)的方式,來組合行為和實(shí)體的一種模式。再來個官方的解釋:
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
網(wǎng)上也有很多資源介紹這個模式,我也不從頭說起了。在.NET中委托給我們給我們提供了簡單實(shí)現(xiàn)策略模式的方法,可以簡單的把一個委托看成是一種策略方法,而且還能借組lmabda表達(dá)式這樣的形式來表達(dá)出來。比如,.NET中對數(shù)組排序的Sort的方法就是一個策略模式的實(shí)現(xiàn)模板。
static void Main(string[] args)
{
int[] array = new int[] { 3, 2, 8, 1, 5 };
//相當(dāng)于是重新設(shè)置了一個排序策略
Array.Sort(array, (a, b) => b - a);
//這里也相當(dāng)于為每個元素安排了一個輸出策略
Array.ForEach(array, Console.WriteLine);
}
以上Array的兩個方法都可以看成是策略模式在.net中的一種實(shí)現(xiàn)。
之前寫一些UI自動化的時候,也借鑒了一下策略模式的思想。下面是我的一個實(shí)例:被測試網(wǎng)站是一個針對全球很多市場的一個網(wǎng)站,有時同一個測試點(diǎn),需要我們配置一下網(wǎng)絡(luò)代理和其它不同的設(shè)置來模擬當(dāng)?shù)厥袌觥?
using System;
using System.Linq;
namespace StrategyPattern
{
class Program
{
static void Main(string[] args)
{
UITest test = new UITest();
test.RunTest();
test.SetProxy("zh-cn");
test.RunTest();
}
}
class UITest
{
Action proxyStrategy;
//Default is US market
public UITest(String market = "en-us")
{
setProxy(market);
}
public void SetProxy(String market)
{
setProxy(market);
}
private void setProxy(String market)
{
Type proxy = typeof(Proxy);
var m = (from i in proxy.GetMethods()
from j in i.GetCustomAttributes(false)
let k = j as Market
where k != null
&& k.MarketName.Contains(market)
select i).First();
proxyStrategy = (Action)Delegate.CreateDelegate(typeof(Action), null, m);
}
public void RunTest()
{
proxyStrategy();
//之后運(yùn)行主要的功能測試
//......
}
}
class Market : Attribute
{
public String MarketName { get; set; }
public Market(String marketName)
{
this.MarketName = marketName;
}
}
class Proxy
{
[Market("en-us,es-us")]
public void SetUSProxy()
{
Console.WriteLine("us proxy");
}
[Market("zh-cn")]
public void SetChinaProxy()
{
Console.WriteLine("china proxy");
}
[Market("en-gb")]
public void SetUKProxy()
{
Console.WriteLine("uk proxy");
}
}
}
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
網(wǎng)上也有很多資源介紹這個模式,我也不從頭說起了。在.NET中委托給我們給我們提供了簡單實(shí)現(xiàn)策略模式的方法,可以簡單的把一個委托看成是一種策略方法,而且還能借組lmabda表達(dá)式這樣的形式來表達(dá)出來。比如,.NET中對數(shù)組排序的Sort的方法就是一個策略模式的實(shí)現(xiàn)模板。
復(fù)制代碼 代碼如下:
static void Main(string[] args)
{
int[] array = new int[] { 3, 2, 8, 1, 5 };
//相當(dāng)于是重新設(shè)置了一個排序策略
Array.Sort(array, (a, b) => b - a);
//這里也相當(dāng)于為每個元素安排了一個輸出策略
Array.ForEach(array, Console.WriteLine);
}
以上Array的兩個方法都可以看成是策略模式在.net中的一種實(shí)現(xiàn)。
之前寫一些UI自動化的時候,也借鑒了一下策略模式的思想。下面是我的一個實(shí)例:被測試網(wǎng)站是一個針對全球很多市場的一個網(wǎng)站,有時同一個測試點(diǎn),需要我們配置一下網(wǎng)絡(luò)代理和其它不同的設(shè)置來模擬當(dāng)?shù)厥袌觥?
復(fù)制代碼 代碼如下:
using System;
using System.Linq;
namespace StrategyPattern
{
class Program
{
static void Main(string[] args)
{
UITest test = new UITest();
test.RunTest();
test.SetProxy("zh-cn");
test.RunTest();
}
}
class UITest
{
Action proxyStrategy;
//Default is US market
public UITest(String market = "en-us")
{
setProxy(market);
}
public void SetProxy(String market)
{
setProxy(market);
}
private void setProxy(String market)
{
Type proxy = typeof(Proxy);
var m = (from i in proxy.GetMethods()
from j in i.GetCustomAttributes(false)
let k = j as Market
where k != null
&& k.MarketName.Contains(market)
select i).First();
proxyStrategy = (Action)Delegate.CreateDelegate(typeof(Action), null, m);
}
public void RunTest()
{
proxyStrategy();
//之后運(yùn)行主要的功能測試
//......
}
}
class Market : Attribute
{
public String MarketName { get; set; }
public Market(String marketName)
{
this.MarketName = marketName;
}
}
class Proxy
{
[Market("en-us,es-us")]
public void SetUSProxy()
{
Console.WriteLine("us proxy");
}
[Market("zh-cn")]
public void SetChinaProxy()
{
Console.WriteLine("china proxy");
}
[Market("en-gb")]
public void SetUKProxy()
{
Console.WriteLine("uk proxy");
}
}
}
相關(guān)文章
asp.net CheckBoxList各項(xiàng)最小寬度CSS樣式(兼容性good)
ASP.NET中,CheckBoxList里的選擇都是自動寬度的,屬性時沒有設(shè)置各項(xiàng)寬度的設(shè)置,在IE10、遨游4極速模式及兼容模式下均可正確顯示最小寬度,此樣式除了用于CheckBoxList外,也可用于DIV等2013-04-04
基于.NET的FluentValidation數(shù)據(jù)驗(yàn)證實(shí)現(xiàn)
這篇文章主要介紹了基于.NET的FluentValidation數(shù)據(jù)驗(yàn)證實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
jQuery+Asp.Net實(shí)現(xiàn)省市二級聯(lián)動功能的方法
這篇文章主要介紹了jQuery+Asp.Net實(shí)現(xiàn)省市二級聯(lián)動功能的方法,涉及asp.net數(shù)據(jù)庫讀取與字符串轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
asp.net 簡單驗(yàn)證碼驗(yàn)證實(shí)現(xiàn)代碼
網(wǎng)站開發(fā)一般登錄注冊的時候都要用到了 所以寫下來給大家參考參考2009-09-09
asp.net mvc 動態(tài)編譯生成Controller的方法
本篇文章主要介紹了asp.net mvc 動態(tài)編譯生成Controller的方法,具有一定的參考價值,有興趣的可以了解一下2017-08-08

