Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.32 KB | None | 0 0
  1.     interface INotifyable
  2.     {
  3.         void Notify();
  4.     }
  5.  
  6.     class Observed
  7.     {
  8.         List<INotifyable> _mySubscribers = new List<INotifyable>();
  9.  
  10.         public void DoSomethingCool()
  11.         {
  12.             foreach (var subscriber in _mySubscribers)
  13.             {
  14.                 subscriber.Notify();
  15.             }
  16.         }
  17.  
  18.         public void SubscribeToMe(INotifyable subscriber)
  19.         {
  20.             _mySubscribers.Add(subscriber);
  21.         }
  22.     }
  23.  
  24.     class Observer1 : INotifyable
  25.     {
  26.         public Observer1(Observed observed)
  27.         {
  28.             observed.SubscribeToMe(this);
  29.         }
  30.  
  31.         public void Notify()
  32.         {
  33.             Console.WriteLine("ZOMZ: I just saw something cool!");
  34.         }
  35.     }
  36.  
  37.     class Observer2 : INotifyable
  38.     {
  39.         public Observer2(Observed observed)
  40.         {
  41.             observed.SubscribeToMe(this);
  42.         }
  43.  
  44.         public void Notify()
  45.         {
  46.             Console.WriteLine("OMFG: I just saw something cool!");
  47.         }
  48.     }
  49.  
  50.     class Program
  51.     {
  52.         static void Main(string[] args)
  53.         {
  54.             Observed o = new Observed();
  55.             Observer1 o1 = new Observer1(o);
  56.             Observer2 o2 = new Observer2(o);
  57.  
  58.             o.DoSomethingCool();
  59.  
  60.             Console.Read();
  61.         }
  62.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement