Click here to Skip to main content
15,861,172 members
Articles / Programming Languages / C#
Tip/Trick

Observer Pattern (C#)

Rate me:
Please Sign up or sign in to vote.
4.89/5 (40 votes)
8 Jan 2015CPOL4 min read 159.8K   55   28
Implementing Observer Design Pattern in C#

Introduction

This tip presents a very interesting design pattern- the observer design pattern. I wrote this tip on Google blogs but I am porting it at the appropriate place.

Background

The observer design pattern is yet another, one of my favorite design patterns which falls in the category of "behavioral pattern". Going by its name, we can say that observer is something (objects in case of OOPS) which is looking upon (observing) other object(s). Observer pattern is popularly known to be based on "The Hollywood Principle" which says- "Don’t call us, we will call you." Pub-Sub (Publisher-Subscriber) is yet another popular nickname given to Observer pattern.

Based on the "Hollywood principle", we can make a guess that in observer pattern, there is a special Hollywood celebrity object in which all other common objects are interested. In actual terms in the observer pattern - "There are n numbers of observers (objects) which are interested in a special object (called the subject). Explaining one step further- there are various objects (called observers) which are interested in things going on with a special object (called the subject). So they register (or subscribe) themselves to subject (also called publisher). The observers are interested in happening of an event (this event usually happens in the boundary of subject object) whenever this event is raised (by the subject/publisher) the observers are notified (they have subscribed for the happening of this event- Remember?)

Using the Code

Simple concepts can be made complex by writing unnecessary literature about that. Let’s dig into an actual example and code to clear everything about observer pattern. Consider an online electronics store which has a huge inventory and they keep on updating it. The store wants to update all its users/customers whenever any product arrives in the store. Just reading the problem statement, we can fit in Observer pattern for the above problem-How? The online electronic store is going to be the subject. Whenever the subject would have any addition in its inventory, the observers (customers/users) who have subscribed to store notifications would be notified through email. Let’s look at the code straight away to get an idea of how we can implement Observer pattern in C#.

Subject

C#
public class Subject:ISubject
{
  private List<Observer> observers = new List<Observer>();
  private int _int;
  public int Inventory
  {
    get
    {
       return _int;
    }
    set
    {
       // Just to make sure that if there is an increase in inventory then only we are notifying 
          the observers.
          if (value > _int)
             Notify();
          _int = value;
    }
  }
  public void Subscribe(Observer observer)
  {
     observers.Add(observer);
  }

  public void Unsubscribe(Observer observer)
  {
     observers.Remove(observer);
  }

  public void Notify()
  {
     observers.ForEach(x => x.Update());
  }
}

interface ISubject
{
   void Subscribe(Observer observer);
   void Unsubscribe(Observer observer);
   void Notify();
}

The "Subject" maintains a list of "Observers". Since every Subject would have somewhat these methods, I tied those in an interface. The Subscribe-Unsubscribe method are the ones through which the Observers register-unregister themselves to the Subject. The Notify method is the one which has the responsibility of notifying all the Observers.

Let’s look at the code for the Observer class:

C#
public class Observer:IObserver
{
  public string ObserverName { get;private set; }
  public Observer(string name)
  {
    this.ObserverName = name;
  }
  public void Update()
  {
    Console.WriteLine("{0}: A new product has arrived at the
    store",this.ObserverName);
  }
}

interface IObserver
{
  void Update();
}

The Observer class is pretty dumb as to it only has a property (ObserverName) to distinguish one observer from another and it also has an Update method. This is the method which almost every observer class would have. It is a way for the Observer for getting notified that something happened in the Subject and since the Observer is registered for notification, so it is getting notified through this method.

Let’s look at the main method which would complete our code for using the Observer pattern.

C#
static void Main(string[] args)
{
   Subject subject = new Subject();
   // Observer1 takes a subscription to the store
   Observer observer1 = new Observer("Observer 1");
   subject.Subscribe(observer1);
   // Observer2 also subscribes to the store
   subject.Subscribe(new Observer("Observer 2"));
   subject.Inventory++;
   // Observer1 unsubscribes and Observer3 subscribes to notifications.
   subject.Unsubscribe(observer1);
   subject.Subscribe(new Observer("Observer 3"));
   subject.Inventory++;
   Console.ReadLine();
}

This Main method is straightforward. We create an instance of Subject so that we can subscribe/unsubscribe different observers. When the inventory in the Subject changes, we notify the Observers by their Update method (look at the code in Subject class).

Below is the output of the program…

Image 1

If you notice that after the first notification since Observer1 has unsubscribed, it would not get notified, when the inventory changes next time. But since Observer3 has subscribed to the list of Observers, it gets notified when there is an update in the Subject.

Points of Interest

The above code for implementing Observer pattern is very simple. There are more sophisticated ways through which we can implement the Observer pattern. One way of doing that is through delegates and events which are very natural in implementing an Observer pattern (Take a look at my article on Events & Delegates [Events & Delegates], the code written there is the implementation of observer pattern). C# now itself supports Observable classes/framework which can be used for implementing Observer pattern. Maybe, I will revisit Observer pattern in another article to have a look at that framework. Also, we will talk about how Subject & Observer implement pull notification and push notification.

History

  • Version 1 - (06/05/2014)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
United States United States
I am a software developer by profession presently working as an Lead Software Engineer in New York City. My passion is technology. I like to learn new things every day and improve my skills in terms of technology. I feel software development can be made very interesting if we inculcate the habit of "beautiful thinking".

Comments and Discussions

 
GeneralCoding example Pin
Member 1401154925-Apr-19 21:23
Member 1401154925-Apr-19 21:23 
QuestionToken of thanks! Pin
ashokinnova9-Oct-18 21:46
ashokinnova9-Oct-18 21:46 
AnswerRe: Token of thanks! Pin
Rahul Dhammy16-Oct-18 8:47
professionalRahul Dhammy16-Oct-18 8:47 
GeneralMy vote of 5 Pin
Avi Farah15-Sep-18 0:19
Avi Farah15-Sep-18 0:19 
GeneralRe: My vote of 5 Pin
Rahul Dhammy16-Oct-18 8:46
professionalRahul Dhammy16-Oct-18 8:46 
PraiseSuperb explanation. Very simple and easy to grab the concept Pin
Vivekananthan Pasupathi11-Oct-17 2:53
Vivekananthan Pasupathi11-Oct-17 2:53 
GeneralRe: Superb explanation. Very simple and easy to grab the concept Pin
Rahul Dhammy16-Oct-18 8:45
professionalRahul Dhammy16-Oct-18 8:45 
QuestionExcellently written Pin
Dhirendra Joshi14-Apr-17 3:38
professionalDhirendra Joshi14-Apr-17 3:38 
AnswerRe: Excellently written Pin
Rahul Dhammy22-Apr-17 18:58
professionalRahul Dhammy22-Apr-17 18:58 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun8-Jan-15 19:01
Humayun Kabir Mamun8-Jan-15 19:01 
GeneralRe: My vote of 5 Pin
Rahul Dhammy9-Jan-15 8:44
professionalRahul Dhammy9-Jan-15 8:44 
QuestionExcellent Pin
nam12345678-Jan-15 12:29
professionalnam12345678-Jan-15 12:29 
AnswerRe: Excellent Pin
Rahul Dhammy9-Jan-15 8:44
professionalRahul Dhammy9-Jan-15 8:44 
GeneralVery good article.. Pin
Hatredalper6-Jan-15 3:16
Hatredalper6-Jan-15 3:16 
GeneralRe: Very good article.. Pin
Rahul Dhammy8-Jan-15 5:45
professionalRahul Dhammy8-Jan-15 5:45 
QuestionWhy this when .net gives us the direct IObserver and IObservable interfaces? Pin
Vijendra(VJ)4-Nov-14 0:01
professionalVijendra(VJ)4-Nov-14 0:01 
AnswerRe: Why this when .net gives us the direct IObserver and IObservable interfaces? Pin
Rahul Dhammy6-Nov-14 9:13
professionalRahul Dhammy6-Nov-14 9:13 
GeneralMy vote of 5 Pin
Shmeenimal13-May-14 7:49
Shmeenimal13-May-14 7:49 
GeneralRe: My vote of 5 Pin
Rahul Dhammy13-May-14 20:29
professionalRahul Dhammy13-May-14 20:29 
QuestionJust use Rx Pin
Sacha Barber7-May-14 2:46
Sacha Barber7-May-14 2:46 
AnswerRe: Just use Rx Pin
Rahul Dhammy7-May-14 2:51
professionalRahul Dhammy7-May-14 2:51 
GeneralRe: Just use Rx Pin
Sacha Barber12-May-14 8:30
Sacha Barber12-May-14 8:30 
GeneralRe: Just use Rx Pin
dmjm-h12-May-14 10:53
dmjm-h12-May-14 10:53 
AnswerRe: Just use Rx Pin
George Swan29-May-14 23:29
mveGeorge Swan29-May-14 23:29 
AnswerRe: Just use Rx Pin
FatCatProgrammer12-Jan-15 4:11
FatCatProgrammer12-Jan-15 4:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.