Design Pattern For Beginner: Part-10: Observer Design Pattern

Welcome to the Design Pattern For Beginners article series. In this article series we are discussing various design patterns of software development. This is the 10th presentation and if you are new to this series then I suggest you go through all previous articles.

Let's discuss the importance of the Observer Design Pattern and when it needs to be implemented.

Why observer pattern?

As the name suggests, it's something related to observation. The question is, who is the observer? The observers are nothing but various systems.

The concept is, one or more systems will be the observer simultaneously and if necessary they can start their action. It's like a bodyguard. Right?

Let's talk about a notification system where the user can send notifications in various ways. They may use SMS notification or Mail Notification or Event Log.

Now, all the notification systems will be alive continuosly, and if needed we can use any one of them, or more than one simultaneously. So , if we draw the conclusion, observer pattern is fit that situation where we choose and use systems at run time. Whereas all systems will alive continuosly. Let's try to implement that in code.

Create various notification classes

We are interested in implementing a uniform naming convention. For that we will implement all notification classes from the INotifyObserver Interface. Each notification class will be implementing a Notify() method.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace ObserverPattern  
  6. {  
  7.     interface INotifyObserver  
  8.     {  
  9.         void Notify();  
  10.     }  
  11.     class MailNotify : INotifyObserver  
  12.     {  
  13.         public void Notify()  
  14.         {  
  15.             Console.WriteLine("Notify through Mail");  
  16.         }  
  17.     }  
  18.     class EventNotify : INotifyObserver  
  19.     {  
  20.         public void Notify()  
  21.         {  
  22.             Console.WriteLine("Notify through Event");  
  23.         }  
  24.     }  
  25.     class SMSNotify : INotifyObserver  
  26.     {  
  27.         public void Notify()  
  28.         {  
  29.             Console.WriteLine("Notify through SMS");  
  30.         }  
  31.     }  

Create notifier class
 
This is a very interesting and important part of the Observer Design Pattern. We can say clsNotifier (see the following code) is our control room. From here we will control which kind of notification will execute.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Collections;  
  6. namespace ObserverPattern  
  7. {  
  8.     class clsNotifier  
  9.     {  
  10.         public ArrayList ALNotify = new ArrayList();  
  11.         /// <summary>  
  12.         /// Add object of notification System  
  13.         /// </summary>  
  14.         /// <param name="obj">Object is notification class</param>  
  15.         public void AddService(INotifyObserver obj)  
  16.         {  
  17.             ALNotify.Add(obj);  
  18.         }  
  19.         /// <summary>  
  20.         /// Remove object of notification System  
  21.         /// </summary>  
  22.         /// <param name="obj">Object of notification Calss</param>  
  23.         public void RemoveService(INotifyObserver obj)  
  24.         {  
  25.             ALNotify.Remove(obj);  
  26.         }  
  27.         public void ExecuteNotifier()  
  28.         {  
  29.             foreach (INotifyObserver O in ALNotify)  
  30.             {  
  31.                 //Call all notification System  
  32.                 O.Notify();  
  33.             }  
  34.         }  
  35.     }  
  36. }
AddService() and RemoveService() are two functions by which we can add an object of various notification classes and ExecuteNotifier() will call all the Notify() functions from each notification class.
 
Design client code

This is the last part of the example. We will create client code to set and make decisions about which notification will be fired.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. namespace ObserverPattern  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             //Generate exception to notify all client  
  12.             try  
  13.             {  
  14.                 throw new ApplicationException("This is Exception");  
  15.             }  
  16.             catch (Exception ex)  
  17.             {  
  18.                 INotifyObserver obj1 = new MailNotify();  
  19.                 INotifyObserver obj2 = new SMSNotify();  
  20.                 clsNotifier O = new clsNotifier();  
  21.                 O.AddService(obj1);  
  22.                 O.AddService(obj2);  
  23.                 O.ExecuteNotifier();  
  24.             }  
  25.             Console.ReadLine();  
  26.         }  
  27.     }  

Here, we are generating ApplicationException() and within the Catch block we are creating an object to the MailNotification and SMSnotification class.
 
The executeNotifier() will call all Notify() functions from each notification class.
 
Output

Observer.jpg


Similar Articles