Facebook
From Cute Coyote, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 227
  1. class EventDispatcher
  2. {
  3.     private static Dictionary<string, List<IPlugin>> observers =
  4.         new Dictionary<string, List<IPlugin>>();
  5.  
  6.     public static void Dispatch(string eventName, string obj)
  7.     {
  8.         foreach(IPlugin plugin in observers[eventName])
  9.         {
  10.             plugin.Run(obj);
  11.         }
  12.     }
  13.    
  14. }
  15.  
  16.  
  17. class Chrome
  18. {
  19.     public string CurrentUrl {set; get;}
  20.     public void LoadPage()
  21.     {
  22.         EventDispatcher.Dispatch("page.loaded", this.CurrentUrl);    
  23.     }
  24. }
  25.  
  26. interface IPlugin
  27. {
  28.     void Run(string somethin);
  29. }
  30.  
  31. class AdBlock : IPlugin
  32. {
  33.     public void Run(string somethin)
  34.     {
  35.         Console.WriteLine("now I can try to detect advertisement on the page: " + something);
  36.     }
  37. }
  38.  
  39. EventDispatcher.Connect("page.loaded", new AdBlock());
  40.  
  41. Chrome chrome = new Chrome();
  42. chrome.CurrentUrl = "http://google.com";
  43. chrome.LoadPage();