Advertisement
Caminhoneiro

Observer

Oct 3rd, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8.  
  9.  
  10. //1 Differentiate between the core(or independent) functionality and the optional(or dependent) functionality.The former will act as a publisher, and the later will serve as subscribers.
  11.  
  12. //2 Create the Subscriber interface. In most cases, a single update method is enough.
  13.  
  14. //3 Create the Publisher interface and describe the operations for starting and terminating subscription in it.Remember that publisher should work with subscribers via their common interface.
  15.  
  16. //4 You have to decide where you put the actual subscription list and the implementation of the subscribtion methods.
  17. //Usually, this code looks the same for all types of publishers. So the obvious place to put it is a base abstract class derived directly from a Publisher interface.
  18. //But if you are integrating Observer to an existing class hierarchy, it might be more convenient to create a small helper class that will be maintaining the subscription for specific publishers.
  19.  
  20. //4 Create Concrete Publisher classes.They should send notifications to the whole list of subscribers each time when something important happens inside the object.
  21.  
  22. //5 Implement update methods in Concrete Subscribers. Most subscribers would need some context data about the event.
  23. //It can be passed as an argument to the update method. But there is another option.
  24. //Upon receiving a notification, the subscriber can fetch any data directly from the publisher object.
  25. //In this case, the publisher must pass itself via the update method.The less flexible option is to link a publisher to the subscriber permanently via the constructor.
  26.  
  27. //6 The Client code must create all necessary subscribers and register them with proper publishers.
  28.  
  29.  
  30. namespace Observer
  31. {
  32.     class Program
  33.     {
  34.         //2 Create the Subscriber interface. In most cases, a single update method is enough.
  35.         public interface ISplObserver
  36.         {
  37.             void update(ISplSubject subject);
  38.         }
  39.  
  40.         //3 Create the Publisher interface and describe the operations for starting and terminating subscription in it.Remember that publisher should work with subscribers via their common interface.
  41.         public interface ISplSubject
  42.         {
  43.             void attach(ISplObserver observer);
  44.  
  45.             void detach(ISplObserver observer);
  46.  
  47.             void notify();
  48.         }
  49.  
  50.         //4 Create Concrete Publisher classes.They should send notifications to the whole list of subscribers each time when something important happens inside the object.
  51.         public class Subject : ISplSubject  //******PUBLISHER*******
  52.         {
  53.             public int State { get; set; } = -0;
  54.  
  55.             private List<ISplObserver> _observers = new List<ISplObserver>();
  56.  
  57.             public void attach(ISplObserver observer)
  58.             {
  59.                 Console.Write("Subject: Attached an observer.\n");
  60.                 this._observers.Add(observer);
  61.             }
  62.  
  63.             public void detach(ISplObserver observer)
  64.             {
  65.                 foreach (var elem in _observers)
  66.                 {
  67.                     if (elem == observer)
  68.                     {
  69.                         _observers.Remove(observer);
  70.                         Console.Write("Subject: Detached an observer.\n");
  71.                         break;
  72.                     }
  73.                 }
  74.             }
  75.  
  76.             public void notify()
  77.             {
  78.                 Console.Write("Subject: Notifying observers...\n");
  79.  
  80.                 foreach (var observer in _observers)
  81.                 {
  82.                     observer.update(this);
  83.                 }
  84.             }
  85.  
  86.             public void someBusinessLogic()
  87.             {
  88.                 Console.Write("\nSubject: I'm doing something important.\n");
  89.                 this.State = new Random().Next(0, 10);
  90.  
  91.                 Thread.Sleep(15);
  92.  
  93.                 Console.Write("Subject: My state has just changed to: " + this.State + "\n");
  94.                 this.notify();
  95.             }
  96.         }
  97.  
  98.  
  99.         class ConcreteObserverA : ISplObserver
  100.         {
  101.             public void update(ISplSubject subject)
  102.             {
  103.                 if (!(subject is Subject))
  104.                 {
  105.                     return;
  106.                 }
  107.  
  108.                 if ((subject as Subject).State < 3)
  109.                 {
  110.                     Console.Write("ConcreteObserverA: Reacted to the event.\n");
  111.                 }
  112.             }
  113.         }
  114.  
  115.         class ConcreteObserverB : ISplObserver
  116.         {
  117.             public void update(ISplSubject subject)
  118.             {
  119.                 if (!(subject is Subject))
  120.                 {
  121.                     return;
  122.                 }
  123.  
  124.                 if ((subject as Subject).State == 0 || (subject as Subject).State >= 2)
  125.                 {
  126.                     Console.Write("ConcreteObserverB: Reacted to the event.\n");
  127.                 }
  128.             }
  129.         }
  130.        
  131.         class Client
  132.         {
  133.             public static void ClientCode()
  134.             {
  135.                 var subj = new Subject();
  136.                 var o1 = new ConcreteObserverA();
  137.                 subj.attach(o1);
  138.  
  139.                 var o2 = new ConcreteObserverB();
  140.                 subj.attach(o2);
  141.  
  142.                 subj.someBusinessLogic();
  143.                 subj.someBusinessLogic();
  144.  
  145.                 subj.detach(o2);
  146.  
  147.                 subj.someBusinessLogic();
  148.             }
  149.         }
  150.        
  151.         static void Main(string[] args)
  152.         {
  153.             Console.WriteLine("Start Observer!");
  154.  
  155.             Client.ClientCode();
  156.  
  157.             Console.ReadKey();
  158.         }
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement