Observer Design Pattern explained with a C# Sample

Introduction

 
What is an Observer Design Pattern? The Observer Design Pattern is a Behavioral Pattern used to notify all the objects that are registered/attached/added to the same type of observer.
 

Why use the Observer Design Pattern?

 
When there is a requirement of "Single object change in its state/behavior/Value needs to notify all other objects which are observing the same object".
 
Basically, a single object should notify multiple objects of the same observer type. It's the relation of one object to many others. 
 
Players in this pattern
 
Subject: Provides a contract to add/remove observers
ConcreteSubject: Implements a contract defined by Subject
Observer: Provides a contract for updating objects when there is a change in the subject state/behavior/Value
ConcreteObserver: Implements a contract defined by Observer
 
Example:
 
Problem definition: Design a software solution for a wholesale pen seller. This wholesale pen seller will sell pens to shops. When there is a change in pen price per market demand, it should automatically notify the change in pen price to all shops.
 
The players are:
 
Subject – IPen
ConcreteSubject – Pen
Observer – IShop
ConcreteObserver – Shop
 
Below is the IPen Interface which will define contact for ConcreteSubjects
  1. namespace Observer.Design.Patterns  
  2. {  
  3.     public interface IPen  
  4.     {  
  5.         void Add(IShop shop);  
  6.         void Remove(IShop shop);  
  7.         void Notify();  
  8.     }  
  9. }  
Below is the Pen class which implements IPen Interafce to add/remove Observers and call Notify, which will invoke when there is a change in pen price
  1. using System;  
  2. using System.Collections.Generic;  
  3.   
  4. namespace Observer.Design.Patterns  
  5. {  
  6.     public class Pen  : IPen
  7.     {  
  8.         public Pen(double penPrize)  
  9.         {  
  10.             _penPrize = penPrize;  
  11.         }  
  12.         public List<IShop> shops = new List<IShop>();  
  13.   
  14.         public void Add(IShop shop) => shops.Add(shop);  
  15.   
  16.         public void Remove(IShop shop) => shops.Remove(shop);  
  17.   
  18.         public void Notify()  
  19.         {  
  20.             foreach (IShop shop in shops)  
  21.                 shop.Update(this);  
  22.   
  23.             Console.WriteLine("----------------------------------------------------------");  
  24.         }  
  25.   
  26.         private double _penPrize;  
  27.   
  28.         public double PenPrize  
  29.         {  
  30.             get => _penPrize;  
  31.             set  
  32.             {  
  33.                 if (_penPrize != value)  
  34.                 {  
  35.                     _penPrize = value;  
  36.                     Notify();  
  37.                 }  
  38.             }  
  39.         }  
  40.   
  41.     }  
  42. }  
Below is the IShop (Observer) interface which defines a contract for ConcreteObserver (i.e. in our case Shop)
  1. namespace Observer.Design.Patterns  
  2. {  
  3.     public interface IShop  
  4.     {  
  5.         void Update(Pen pen);  
  6.     }  
  7. }  
Below is the Shop class which implements IShop to update the Subject object
  1. using System;  
  2.   
  3. namespace Observer.Design.Patterns  
  4. {  
  5.     public class Shop : IShop  
  6.     {  
  7.         private string _shopName;  
  8.   
  9.         public Shop(string shopName) => _shopName = shopName;  
  10.   
  11.         public void Update(Pen pen)  
  12.         {  
  13.             Console.WriteLine($" pen prize changed to { pen.PenPrize} in {_shopName}");  
  14.         }  
  15.     }  
  16. }  
We will see the execution and the result now
  1. using System;  
  2.   
  3. namespace Observer.Design.Patterns  
  4. {  
  5.     public class Program  
  6.     {  
  7.         public static void Main()  
  8.         {  
  9.             Pen pen = new Pen(10);  
  10.             pen.Add(new Shop("Shop1"));  
  11.             pen.Add(new Shop("Shop2"));  
  12.             pen.Add(new Shop("Shop3"));  
  13.   
  14.             pen.PenPrize = 20;  
  15.             pen.PenPrize = 30;  
  16.             pen.PenPrize = 40;  
  17.   
  18.             Console.ReadLine();  
  19.         }  
  20.     }  
  21. }  
Below is the result screenshot. In the result, we can see that change in pen price value has notified all shops
 
 
 
Summary
 
In this article, we have learned through code example what Observer design pattern is, as well as why and where to use it.