Click here to Skip to main content
13,089,401 members (60,071 online)
Click here to Skip to main content
Add your own
alternative version


Posted 19 Feb 2013

The Observer Pattern

, 19 Feb 2013
Rate this:
Please Sign up or sign in to vote.
The "Observer Pattern" defines a One-To-Many dependency between objects.


The "Observer Pattern" defines a One-To-Many dependency between objects.

when one object changes state, all of its dependentsare notified and updated automatically.

ONE Subject to the MANY Observers !!!!
Subject can be anything which interests Observers.
Observers are objects who are interested in some Subject.

EXAMPLE : Newpaper Subscription where "Newspaper is Subject" and "Subscribers are Observers".
          ONE Newspaper to MANY Subscribers.


Design Principal  : Strive for loosely coupled designs between objects that interact.

Loosely coupled designs allow us to build flexible system that can handle changes becoz they minimize interdependency.

Note : Observer Pattern can be implemented using Events & Delegates in .Net, .Net 4.0 has now IObservable & IObserver interfaces to facilitate the Observer Pattern.
This Example has implementations using Custom Interface & .Net in-house IObservable interface.

Using the code

// Custom IObserver Interface

public interface IObserver
    void Update(float temp, float hum, float press);

 // Observer Class which Observes or Subscribe from Observable Data

public class CurrentConditionsDisplay : IObserver,IDisplayElement
    private float temperature;
    private float humidity;
    private ISubject weatherData;

    public CurrentConditionsDisplay(ISubject weatherData)
        this.weatherData = weatherData;
        if(weatherData != null)

    public void RemoveObservation()

    public void Update(float temp, float hum, float press)
        this.temperature = temp;
        this.humidity = hum;

    public void Display()
        Console.WriteLine("Current Conditions: " + temperature + "F Degrees and " + humidity + "% Humidity");


// Custom IObservable Interface

public interface ISubject
    void RegisterObserver(IObserver observer);
    void RemoveObserver(IObserver observer);
    void NotifyObservers();


// Observable class which can be observed by Observer Class

public class WeatherData : ISubject
    private ArrayList Observers;
    private float _temperature;
    private float _humidity;
    private float _pressure;

    public WeatherData()
        Observers = new ArrayList();

    #region ISubject Members

    public void RegisterObserver(IObserver observer)

    public void RemoveObserver(IObserver observer)
        int i = Observers.IndexOf(observer);
        if (i >= 0)

    public void NotifyObservers()
        foreach (IObserver o in Observers)
            o.Update(_temperature, _humidity, _pressure);


    public void MeasurementChanged()

    public void SetMeasurements(float temperature, float humidity, float pressure)
        this._temperature = temperature;
        this._humidity = humidity;
        this._pressure = pressure;

Points of Interest

This Pattern can be implemented by directly using IObserver<T> & IObservable<T> provided by .Net Library also, attached code includes implementation with .Net in-house interface as well.


Version 1


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


About the Author

Chandra Shekhar Joshi
Software Developer (Senior) ABB
India India
Working in Software Development since 2007.
Work in .Net/VC++/COM.
work in Process Automation domain.

You may also be interested in...


Comments and Discussions

Question[My vote of 1] My vote of 1. Pin
r v19-Feb-13 4:01
memberr v19-Feb-13 4:01 
AnswerRe: [My vote of 1] My vote of 1. Pin
Chandra Shekhar Joshi19-Feb-13 15:03
memberChandra Shekhar Joshi19-Feb-13 15:03 
QuestionNot an article Pin
OriginalGriff18-Feb-13 23:05
mvpOriginalGriff18-Feb-13 23:05 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170813.1 | Last Updated 19 Feb 2013
Article Copyright 2013 by Chandra Shekhar Joshi
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid