This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Advantages of using delegates

By: a guest on Feb 28th, 2012  |  syntax: None  |  size: 2.58 KB  |  views: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. enum TaskStatus
  2. {
  3.    Started,
  4.    StillProcessing,
  5.    Finished
  6. }
  7.  
  8. delegate void CallbackDelegate(Task t, TaskStatus status);
  9.  
  10. class Task
  11. {
  12.     public void Start(CallbackDelegate callback)
  13.     {
  14.         callback(this, TaskStatus.Started);
  15.  
  16.         // calculate PI to 1 billion digits
  17.         for (...)
  18.         {
  19.             callback(this, TaskStatus.StillProcessing);
  20.         }
  21.  
  22.         callback(this, TaskStatus.Finished);
  23.     }
  24. }
  25.  
  26. class Program
  27. {
  28.     static void Main(string[] args)
  29.     {
  30.         Task t = new Task();
  31.         t.Start(new CallbackDelegate(MyCallbackMethod));
  32.     }
  33.  
  34.     static void MyCallbackMethod(Task t, TaskStatus status)
  35.     {
  36.         Console.WriteLine("The task status is {0}", status);
  37.     }
  38. }
  39.        
  40. private delegate void UpdateHandler(string message);
  41. private UpdateHandler Update;
  42.  
  43. public void Register(IObserver observer)
  44. {
  45.     Update+=observer.Update;
  46. }
  47.  
  48. public void Unregister(IObserver observer)
  49. {
  50.     Update-=observer.Update;
  51. }
  52.  
  53. public void Notify(string message)
  54. {
  55.     Update(message);
  56. }
  57.        
  58. public Subject()
  59. {
  60.     observers = new List<IObserver>();
  61. }
  62.  
  63. public void Register(IObserver observer)
  64. {
  65.     observers.Add(observer);
  66. }
  67.  
  68. public void Unregister(IObserver observer)
  69. {
  70.     observers.Remove(observer);
  71. }
  72.  
  73. public void Notify(string message)
  74. {
  75.     // call update method for every observer
  76.     foreach (IObserver observer in observers)
  77.     {
  78.         observer.Update(message);
  79.     }
  80. }
  81.        
  82. public delegate void UpdateHandler(string message);
  83. private UpdateHandler Update;
  84.  
  85. public void Register(UpdateHandler observerRoutine)
  86. {
  87.     Update+=observerRoutine;
  88. }
  89.  
  90. public void Unregister(UpdateHandler observerRoutine)
  91. {
  92.     Update-=observerRoutine;
  93. }
  94.  
  95. public void Notify(string message)
  96. {
  97.     Update(message);
  98. }
  99.        
  100. public delegate string TestDelegate();
  101. protected void Page_Load(object sender, EventArgs e)
  102. {
  103.     TestDelegate TD1 = new TestDelegate(DiaplayMethodD1);
  104.     TestDelegate TD2 = new TestDelegate(DiaplayMethodD2);
  105.     TD2 = TD1 + TD2; // Make TD2 as multi-cast delegate
  106.     lblDisplay.Text =  TD1(); // invoke delegate
  107.     lblAnotherDisplay.Text = TD2();
  108.  
  109.  
  110.     // Note: Using a delegate allows the programmer to encapsulate a reference
  111.     //       to a method inside a delegate object. Its like the function pointer
  112.     //       in C or C++.    
  113. }
  114. //the Signature has to be same.
  115. public string DiaplayMethodD1()
  116. {
  117.     //lblDisplay.Text = "Multi-Cast Delegate on EXECUTION"; // Enable on multi-cast
  118.     return "This is returned from the first method of delegate explanation";
  119. }
  120. // The Method can be static also
  121. public static string DiaplayMethodD2()
  122. {
  123.     return " Extra words from second method";
  124. }
clone this paste RAW Paste Data