Click here to Skip to main content
14,735,317 members
Articles » Development Lifecycle » Design and Architecture » Patterns and Practices
Posted 6 May 2014

Tagged as


55 bookmarked

Observer Pattern (C#)

Rate me:
Please Sign up or sign in to vote.
4.89/5 (40 votes)
8 Jan 2015CPOL
Implementing Observer Design Pattern in C#


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.


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#.


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

  public void Unsubscribe(Observer 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:

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

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.

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

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.


  • Version 1 - (06/05/2014)


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


About the Author

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