Observer design pattern

This short article shows you have to build Observer design pattern in C#.

After all the studies done on the web site. The one that easiest to understand is still from This article extract the code structure to illustrate how to build Observer design pattern.

Observer design pattern provides a way of notifying change to a number of classes.

Logical diagram of Observer designer pattern below.


First create a Subject, abstract, class that knows its observers. Any number of Observer objects may observe a subject. This class provides an interface for attaching and detaching Observer objects.

abstract class Subject


    private List<Observer> _observers = new List<Observer>();


    public void Attach(Observer observer)





    public void Detach(Observer observer)





    public void Notify()


        foreach (Observer o in _observers)







Secondly, create a ConcreteSubject class that is inherit from the Subject class. This class stores state of interest to ConcreteObserver and sends a notification to its observers when its state changes.

class ConcreteSubject : Subject


    private string _subjectState;


    // Gets or sets subject state

    public string SubjectState


        get { return _subjectState; }

        set { _subjectState = value; }




Now, create an Observer, abstract, that defines an updating interface for objects that should be notified of changes in a subject.

abstract class Observer


    public abstract void Update();



Finally, create a ConcreteObserver to maintain a reference to a ConcreteSubject object and stores state that should stay consistent with the subject’s. This class also implements the Observer updating interface to keep its state consistent with the subject’s.

class ConcreteObserver : Observer


    private string _name;

    private string _observerState;

    private ConcreteSubject _subject;


    // Constructor

    public ConcreteObserver(

      ConcreteSubject subject, string name)


        this._subject = subject;

        this._name = name;



    public override void Update()


        _observerState = _subject.SubjectState;

        Console.WriteLine(“Observer {0}‘s new state is {1},

          _name, _observerState);



    // Gets or sets subject

    public ConcreteSubject Subject


        get { return _subject; }

        set { _subject = value; }




The main program as follow.

static void Main()


    // Configure Observer pattern

    ConcreteSubject s = new ConcreteSubject();


    s.Attach(new ConcreteObserver(s, “X”));

    s.Attach(new ConcreteObserver(s, “Y”));

    s.Attach(new ConcreteObserver(s, “Z”));


    // Change subject and notify observers

    s.SubjectState = “ABC”;



    // Wait for user




You can also download a single solution from The sample file name is My MSN ID is

About chanmingman

Since March 2011 Microsoft Live Spaces migrated to Wordpress ( till now, I have is over 1 million viewers. This blog is about more than 50% telling you how to resolve error messages, especial for Microsoft products. The blog also has a lot of guidance teaching you how to get stated certain Microsoft technologies. The blog also uses as a help to keep my memory. The blog is never meant to give people consulting services or silver bullet solutions. It is a contribution to the community. Thanks for your support over the years. Ming Man is Microsoft MVP since year 2006. He is a software development manager for a multinational company. With 25 years of experience in the IT field, he has developed system using Clipper, COBOL, VB5, VB6, VB.NET, Java and C #. He has been using Visual Studio (.NET) since the Beta back in year 2000. He and the team have developed many projects using .NET platform such as SCM, and HR based applications. He is familiar with the N-Tier design of business application and is also an expert with database experience in MS SQL, Oracle and AS 400.
This entry was posted in .Net and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s