2016年4月19日 星期二

【C#】Event & Delegate


Event (事件)
是方法的一種,一般是 UI 行為引發的方法,比方
  1. private void Button_Click(object sender, EventArgs e)
  2. {
  3. ...
  4. }
當按鈕按下就會觸發,sender 即按鈕 instance,e 是事件參數,在特定事件行為有機會用到,例如
  1. private void DataGridView_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
  2. {
  3. if(e.RowIndex > 0)
  4. ...
  5. }
這是 WinForm DataGridView 的資料列驗證事件,這裡 e 提供了 RowIndex 屬性讓我們可以知道現在驗證中的具體是哪一列。

當然事件是可以自己寫,比方自己動態產生的多個按鈕需要指派不同的事件就需要自己去寫,不過大部分的情況我們會是用系統建出來的。

接著,讓我們走進細節;具體來說 .Net 中事件必須是 event 形式的委派。

delegate (委派)
是一個方法類別化、變數化的做法,看程式比較容易懂
  1. delegate void PrinterMethodClass(string msg);
  2. static void PrintMethod1(string msg)
  3. {
  4. Console.WriteLine("type1: " + msg);
  5. }
  6. static void PrintMethod2(string msg)
  7. {
  8. Console.WriteLine("type2: " + msg);
  9. }
  10. static void Main()
  11. {
  12. PrinterMethodClass Print = new PrinterMethodClass(PrintMethod1);
  13. Print.Invoke("some message.");
  14. Console.ReadLine();
  15. }
其中 PrinterMethodClass 是委派(方法的 class)
PrintMethod1、PrintMethod2 是符合 PrinterMethodClass 方法簽名與回傳值的實作(方法的 instance)
Print 是宣告為特定委派的變數(方法的 variable),其實際運行的是 PrintMethod1 instance。

這個設計的目的在於提供外部注入方法的彈性
  1. static void Foo(PrinterMethodClass concretePrint)
  2. {
  3. var print = new PrinterMethodClass(concretePrint);
  4. print.Invoke("some message.");
  5. Console.ReadLine();
  6. }
和附加自己的事件到 UI 的事件上。

Event (事件) #2
我們已經知道委派長怎樣,事件相對的會是像
  1. static event EventHandler _eh;
其中 event 等同 delegate,但是它指定了這個委派屬於事件,方法簽名與回傳值格式必為 void (object sender, EventArgs e),所以也不需要像前面的 void (string msg)。
EventHandler 相當於 PrinterMethodClass,但它是所有 .Net 事件的 class。

EventHandler(事件處理函式)
handle 是從「把手」發展來的講法,可以想成遊戲的手把,接一條線去控制主機。
  1. static event EventHandler _eh;
  2.  
  3. static void Main()
  4. {
  5. ConsoleKeyInfo keyinfo;
  6. do
  7. {
  8. keyinfo = Console.ReadKey();
  9. if (keyinfo.Key == ConsoleKey.UpArrow)
  10. {
  11. _eh = new EventHandler(Up);
  12. }
  13. else if (keyinfo.Key == ConsoleKey.DownArrow)
  14. {
  15. _eh = new EventHandler(Down);
  16. }
  17. else if (keyinfo.Key == ConsoleKey.LeftArrow)
  18. {
  19. _eh = new EventHandler(Left);
  20. }
  21. else if (keyinfo.Key == ConsoleKey.RightArrow)
  22. {
  23. _eh = new EventHandler(Right);
  24. }
  25. _eh.Invoke(new object(), new EventArgs());
  26. }
  27. while (keyinfo.Key != ConsoleKey.X);
  28. }
  29.  
  30. static void Up(object sender, EventArgs e)
  31. {
  32. Console.Write("↑ ");
  33. }
  34.  
  35. static void Down(object sender, EventArgs e)
  36. {
  37. Console.Write("↓ ");
  38. }
  39.  
  40. static void Left(object sender, EventArgs e)
  41. {
  42. Console.Write("← ");
  43. }
  44.  
  45. static void Right(object sender, EventArgs e)
  46. {
  47. Console.Write("→ ");
  48. }
因為是事件用的所以 sender 跟 EventArgs 是必要的。

匿名方法 (Anonymous Method)
實務上指派給委派變數的方法常常就只會用在這麼一個地方,比方 PrintMethod1() 要拆開寫在Main()外面又沒有別的地方會用到,其實有點多餘所以
  1. PrinterMethodClass Print = delegate(string msg)
  2. {
  3. Console.WriteLine("anonymous type: " + msg);
  4. };
其實可以寫成這樣,delegate(string msg) 即是一個匿名方法,因為假定別的地方不會用到所以方法名稱就省下來了,直接把整個方法 instance 指給委派變數。

Lambda
就是高度簡化的匿名方法,最常應用於 Linq,比如前述的式可以再簡化
  1. PrinterMethodClass Print = (string msg) =>
  2. {
  3. Console.WriteLine("anonymous type: " + msg);
  4. };
delegate 字樣就省下來了,因為只有一行所以
  1. PrinterMethodClass Print = (string msg) => Console.WriteLine("anonymous type: " + msg);
可以這樣,又因為只有一個參數所以
  1. PrinterMethodClass Print = msg => Console.WriteLine("anonymous type: " + msg);
連括號跟型別都可以省掉,我個人是覺得有點省過頭了,對第一次看到不知道這個演進流程的人來說,應該是一頭霧水。我自己是參考 這篇 跟 這篇,真的是少數寫給人類看的文章。

CallBack (回呼)
一個 CallBack,是指一個被當成引數傳進其他方法的 method instance,在得到該 instance 的方法中達成特定條件後才會觸發這個 instance。 這個設計主要的功能在預期需要擴充的類似功能方法組合從業務邏輯類別裡隔離出來,例如
  1. public class Program
  2. {
  3. public delegate string GetName();
  4.  
  5. static void Main()
  6. {
  7. string input = Console.ReadLine();
  8. GetName MethodFromExpand = ExpandClass.GetMethod(input);
  9. PrintName(MethodFromExpand());
  10.  
  11. Console.ReadLine();
  12. }
  13.  
  14. private static void PrintName(string output)
  15. {
  16. Console.WriteLine(output);
  17. }
  18. }
  19.  
  20. public class ExpandClass
  21. {
  22. public static MyLab.Program.GetName GetMethod(string input)
  23. {
  24. switch (input)
  25. {
  26. case "ENG":
  27. return GetEngName;
  28. default:
  29. return GetChineseName;
  30. //break;
  31. }
  32. }
  33.  
  34. private static string GetChineseName()
  35. {
  36. return "王小明";
  37. }
  38.  
  39. private static string GetEngName()
  40. {
  41. return "HSIAO MING WANG";
  42. }
  43. }
如前例,當我要再擴充其他語系的 GetName 就只需要擴在 ExpandClass 即可,能保持主程式業務邏輯的乾淨。

※ 額外補充,在 javascript 中 Callback 的作法,除了方法作為參數傳遞之外,另一個很重要的用途是讓傳遞進去的方法實體在接收端方法完成後才執行,因為 javascript 是邊編譯邊執行所以需要這樣作流程控制,這個部分有興趣的朋友可以自己搜尋一下。

Action 與 Func
考慮到 Callback 在現代的程式設計已經算頗常用的功能,每一次要自己宣告 delegate,且幾乎都只會用在同個地方,實在是很搞剛,所以 .NET 提供了 Action 與 Func 兩個固定的委派來簡化其處理,比如上面的程式就可以改成
  1. public class Program
  2. {
  3. //public delegate string GetName(); 不需要
  4.  
  5. static void Main()
  6. {
  7. string input = Console.ReadLine();
  8. Func<string> MethodFromExtand = ExpandClass.GetMethod(input); // 由 Func 取代
  9. PrintName(MethodFromExtand());
  10.  
  11. Console.ReadLine();
  12. }
  13. ...
  14. }
  15.  
  16. public class ExpandClass
  17. {
  18. public static Func<string> GetMethod(string input) // 由 Func 取代
  19. {
  20. ...
  21. }
其中 Action 跟 Func 的不同只在於 Action 是用於沒有回傳值的方法 Action<T>、Action<T, T>等,T為參數,而 Func 是用於有回傳值的情況 Fun <TResult>、Func<T, TResult>等,TResult 為回傳值。

沒有留言:

張貼留言