一、什么是委托
1.1官方解釋
委托是一種定義方法簽名的類型。當實例化委托時,您可以將其實例與任何具有兼容簽名的方法相關聯。您可以通過委托實例調用方法。
1.2個人理解
委托就是執行方法(函數)的一個類。
事件是一種特殊的委托。
二、如何申明委托
2.1 delegate
public delegate int TestDelegate(int x, int y);
2.2 Action
Action是無返回值的泛型委托。
Action 表示無參,無返回值的委托
Action<int,string> 表示有傳入參數int,string無返回值的委托
2.3 Func
Func是有返回值的泛型委托
Func<int> 表示無參,返回值為int的委托
Func<object,string,int> 表示傳入參數為object, string 返回值為int的委托
2.4 predicate
predicate 是返回bool型的泛型委托
predicate<int> 表示傳入參數為int 返回bool的委托。
2.5 四者之間的區別
Delegate至少0個參數,至多32個參數,可以無返回值,也可以指定返回值類型
Action至少1個參數,至多4個參數,無返回值,
Func至少0個參數,至多4個參數,根據返回值泛型返回。必須有返回值,不可void
Predicate至少1個參數,至多1個參數,返回值固定為bool
三、如何使用委托
3.1 Labmda表達式
TestDelegate d2= (string name) => { Console.WriteLine("你好,{0}!", name); };
d2("Terry");
3.2匿名方法
delegate void TestDelegate(string myName);
TestDelegate d2 = delegate(string name)
{
Console.WriteLine("Hello,{0}!", name);
};
d2(“Test”);
3.3 函數申明
private void DelegateMethod(string name)
{
Console.WriteLine("Hello,{0}!", name);
}
TestDelegate d2 = new TestDelegate(DelegateMethod);
d2(“Test”);
四、使用委托有哪些特點
委托類似於 C++ 函數指針,但它們是類型安全的。
委托允許將方法作為參數進行傳遞。
委托可用於定義回調方法。
委托可以鏈接在一起;例如,可以對一個事件調用多個方法。
方法不必與委托簽名完全匹配。
五、委托使用場景
委托一般都使用在 Observer模式(觀察者模式)。
Observer設計模式是為了定義對象間的一種一對多的依賴關系,以便於當一個對象的狀態改變時,其他依賴於它的對象會被自動告知並更新。
Observer模式主要包括如下兩類對象:
被監視對象:往往包含着其他對象所感興趣的內容。
監視者:當對象中的某件事發生的時候,會告知建設者,而建設者則會采取相應的行動。
例如:當你程序處理大批量數據時,需要在程序界面顯示進度條進行友好提示,這時你通過委托來實現相當方便。
范例:
public delegate void DelegateMethod(int position, int maxValue);
public class TestDelegate
{
public DelegateMethod OnDelegate;
public void DoDelegateMethod()
{
int maxValue = 100;
for (int i = 0; i < maxValue; i++)
{
if (this.OnDelegate != null)
{
this.OnDelegate(i, maxValue);
}
}
}
}
TestDelegate test = new TestDelegate();
this.textBox1.Text = "";
this.progressBar1.Value = 0;
test.OnDelegate = new DelegateMethod(delegate(int i, int maxValue)
{
this.textBox1.Text += i.ToString() + Environment.NewLine;
this.progressBar1.Maximum = maxValue;
this.progressBar1.Value++;
});
test.DoDelegateMethod();
六、如何清空委托
1、在類中申明清空委托方法,依次循環去除委托引用。
方法如下:
public class TestDelegate
{
public DelegateMethod OnDelegate;
public void ClearDelegate()
{
while (this.OnDelegate != null)
{
this.OnDelegate -= this.OnDelegate;
}
}
}
2、如果在類中沒有申明清空委托的方法,我們可以利用GetInvocationList查詢出委托引用,然后進行去除。
方法如下:
TestDelegate test = new TestDelegate();
if (test.OnDelegate != null)
{
System.Delegate[] dels = test.OnDelegate.GetInvocationList();
for (int i = 0; i < dels.Length; i++)
{
test.OnDelegate -= dels[i] as DelegateMethod;
}
}
七、實戰范例
功能需求:查詢打印機的墨粉量,如果低於50時則發送Email郵件到客戶進行提醒。
優化前代碼
namespace DelegateExample.Before
{
public class SpyPrinterToner
{
public void CheckPrinterTonerIsLower()
{
PhysicalPrinterAction action = new PhysicalPrinterAction();
int remainToner = action.SelectPrinterToner();
if (remainToner < 50)
{
MessageController controller = new MessageController();
controller.SendMessage("Printer Name");
}
}
}
public class MessageController
{
public void SendMessage(string printerName)
{
//TODO: SendMessage
}
}
public class PhysicalPrinterAction
{
public int SelectPrinterToner()
{
return 80;
}
}
}
調用:
DelegateExample.Before.SpyPrinterToner toner = new Before.SpyPrinterToner();
toner.CheckPrinterTonerIsLower();
以上代碼也可以說采用了面向對象編程,但是SpyPrinterToner 與 MessageController 之間存在了不必要的耦合度, 造成了日后的程序維護的工作量以及不便於程序的擴展。
那我們該如何降低 SpyPrinterToner 與 MessageController 之間的耦合度,從而達到:高內聚,低耦合的目的。
顯而易見我們利用觀察者模式可以達到。
優化后的代碼
namespace DelegateExample.After
{
public class SpyPrinterToner
{
public Action<string> OnSendMessage;
public void CheckPrinterTonerIsLower()
{
PhysicalPrinterAction action = new PhysicalPrinterAction();
int remainToner = action.SelectPrinterToner();
if (remainToner < 50)
{
if (this.OnSendMessage != null)
{
this.OnSendMessage("Printer Name");
}
}
}
}
public class MessageController
{
public void SendMessage(string printerName)
{
//TODO: SendMessage
}
}
public class PhysicalPrinterAction
{
public int SelectPrinterToner()
{
return 80;
}
}
}
調用
DelegateExample.After.SpyPrinterToner toner = new After.SpyPrinterToner();
toner.OnSendMessage += new Action<string>(new After.MessageController().SendMessage);
toner.CheckPrinterTonerIsLower();
進行這樣的優化之后,2個類直接的耦合度降低了。
如果日后需求進行了更改,需要增加IM類型的消息或者其他類型的消息類別,那我們則只需要再增加一個委托即可,如果不采用委托去實現,則SpyPrinterToner類又會與IM處理類或者其他類相互耦合。
八、利用Func委托代碼優化
在項目開發過程中經常會看到類似的代碼:
try
{
Do();
}
catch (Exception ex)
{
LogException(ex);
}
finally
{
DoFinally();
}
造成代碼量的冗余,給日后代碼維護帶來很多的不便。
有很多種方法可以實現,例如:AOP、委托等。在這里我們主要講如何利用Func委托來實現代碼優化。
private void CallMethod(Func<string> func)
{
try
{
func();
}
catch (Exception ex)
{
LogException(ex);
}
finally
{
DoFinally();
}
}
CallMethod(new Func<string>(Do));
我們將方法作為委托進行傳入,這樣節省了很多的冗余代碼。