一、什麼是委託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));
我們將方法作為委託進行傳入,這樣節省了很多的冗餘代碼。