.NET 下運用策略模式(組合行為和實體的一種模式)
更新時間:2012年12月30日 12:39:05 作者:
我簡單的理解策略模式就是把行為(方法)單獨的抽象出來,并采用組合(Has-a)的方式,來組合行為和實體的一種模式比如,.NET中對數(shù)組排序的Sort的方法就是一個策略模式的實現(xiàn)模板
我簡單的理解策略模式就是把行為(方法)單獨的抽象出來,并采用組合(Has-a)的方式,來組合行為和實體的一種模式。再來個官方的解釋:
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中委托給我們給我們提供了簡單實現(xiàn)策略模式的方法,可以簡單的把一個委托看成是一種策略方法,而且還能借組lmabda表達式這樣的形式來表達出來。比如,.NET中對數(shù)組排序的Sort的方法就是一個策略模式的實現(xiàn)模板。
static void Main(string[] args)
{
int[] array = new int[] { 3, 2, 8, 1, 5 };
//相當于是重新設(shè)置了一個排序策略
Array.Sort(array, (a, b) => b - a);
//這里也相當于為每個元素安排了一個輸出策略
Array.ForEach(array, Console.WriteLine);
}
以上Array的兩個方法都可以看成是策略模式在.net中的一種實現(xiàn)。
之前寫一些UI自動化的時候,也借鑒了一下策略模式的思想。下面是我的一個實例:被測試網(wǎng)站是一個針對全球很多市場的一個網(wǎng)站,有時同一個測試點,需要我們配置一下網(wǎng)絡(luò)代理和其它不同的設(shè)置來模擬當?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();
//之后運行主要的功能測試
//......
}
}
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中委托給我們給我們提供了簡單實現(xiàn)策略模式的方法,可以簡單的把一個委托看成是一種策略方法,而且還能借組lmabda表達式這樣的形式來表達出來。比如,.NET中對數(shù)組排序的Sort的方法就是一個策略模式的實現(xiàn)模板。
復制代碼 代碼如下:
static void Main(string[] args)
{
int[] array = new int[] { 3, 2, 8, 1, 5 };
//相當于是重新設(shè)置了一個排序策略
Array.Sort(array, (a, b) => b - a);
//這里也相當于為每個元素安排了一個輸出策略
Array.ForEach(array, Console.WriteLine);
}
以上Array的兩個方法都可以看成是策略模式在.net中的一種實現(xiàn)。
之前寫一些UI自動化的時候,也借鑒了一下策略模式的思想。下面是我的一個實例:被測試網(wǎng)站是一個針對全球很多市場的一個網(wǎng)站,有時同一個測試點,需要我們配置一下網(wǎng)絡(luò)代理和其它不同的設(shè)置來模擬當?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();
//之后運行主要的功能測試
//......
}
}
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 文件上傳與刷新與asp.net頁面與iframe之間的數(shù)據(jù)傳輸
眾所周知微軟所提供的updatepanel不能支持文件上傳的異步刷新,但是往往當你在項目中的其他頁面實現(xiàn)了異步刷新之后,客戶就會問你為什么有文件上傳的頁面就不能實現(xiàn)異步刷新呢?這時我們可能說一堆理由,但是最后大部分還是會妥協(xié)于客戶。2009-12-12DataGridView使用BindingNavigator實現(xiàn)簡單分頁功能
這篇文章主要介紹了DataGridView使用BindingNavigator實現(xiàn)簡單分頁功能,本文主要是通過借用BindingNavigator空殼,文中通過實例代碼講解的非常詳細,需要的朋友可以參考下2019-11-11asp.net+Ajax校驗用戶是否存在的實現(xiàn)代碼
主要技術(shù)點 jquery ajax以及blur事件,當用戶名輸入框失去焦點的時候就會觸發(fā)blur事件,然后進行ajax請求,獲得結(jié)果(true或者false),如果請求結(jié)果為true,就把用戶名輸入框圖片替換成ok,并且輸出文字:恭喜您2012-05-05C# 中使用iTextSharp組件創(chuàng)建PDF的簡單方法
C# 中使用iTextSharp組件創(chuàng)建PDF的簡單方法,需要的朋友可以參考一下2013-03-03ASP.NET Core Api網(wǎng)關(guān)Ocelot的使用初探
這篇文章主要介紹了ASP.NET Core Api網(wǎng)關(guān)Ocelot的使用初探,幫助大家更好的理解和學習使用.NET技術(shù),感興趣的朋友可以了解下2021-03-03