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