Introduction to the observer design pattern

No.of Views1208
Bookmarked0 times
By  Chinna Srihari   On  23 Oct 2010 03:10:09
Tag : Design Patterns , General
A way of notifying change to number of depended class.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at

A way of notifying change to number of depended class.
The Observer pattern defines a one to many relationships between objects so that when one changes its state, all the others are notified accordingly.
Encapsulate the core components in a Subject abstraction, and the variable components in an Observer hierarchy.
The Observer pattern is composed of two classes. The Subject/Target is a class whose objects change their state at an independent rate. Observers may indicate that they wish to be informed of these changes the Subject/Target will send them notifications.
UML Diagram
 Please refer to the attached doc for the imapges
    public delegate void ChangedEventHandler(object sender, ChangedEventArgs e);
    public class Target: ArrayList
        public event ChangedEventHandler Changed;
        public override int Add(object value)
            if (Changed != null)
Changed(this, new ChangedEventArgs() { PropertName = "Add", Value = value });
            return base.Add(value);
    //Custom Event args
    public class ChangedEventArgs : EventArgs
        public string PropertName { get; set; }
        public object Value { get; set; }
public class ObserverOne
        public ObserverOne(Target target)
            target.Changed += new ChangedEventHandler(A);
        public void A(object sender, ChangedEventArgs e)
            Console.WriteLine("Tagret added with " + e.Value.ToString());
    public class ObserverTwo
        public ObserverTwo(Target target)
            target.Changed += new ChangedEventHandler(A);
        public void A(object sender, ChangedEventArgs e)
            Console.WriteLine("Tagret added with " + e.Value.ToString());
static void Main(string[] args)
            Target target = new Target();
            ObserverOne observer1 = new ObserverOne(target);
            ObserverTwo observer2 = new ObserverTwo(target);

Hope this helps


Download Sample

Download source files -58 kb

Sign Up to vote for this article
About Author
Chinna Srihari
Occupation-Software Engineer
Member Type-Junior
Joined date-17 Jun 2010
Home Page-
Blog Page-
Having 12 + years of IT experience, 2.5 years as Technical Management experience and 1.5 year of onsite coordinator experience and rest 8+ years of technical activities including Project Leader, Tech Lead, Designer, PQL, and Developer experience in Quality Management System (CMM-level 5, SEI-CMM-level 5).
Other popularSectionarticles
    This is a quite simple design pattern, let me tell you one thing there are many design patterns that they are implicitly singletons. I rather recommend to understanding this one first before going through other patterns.
    Published Date : 21/Oct/2010
    As a developer we might have noticed that creation of some class is very difficult and number of time that we need to create. Such object creation every time is very expensive in terms of system resources. If we can cache that kind of object show how will surely boost the application performance and resource usage can be saved. This where object pool design pattern will help development community to cache the objects. Some time it is also called as Object cache or Resource cache design pattern.
    Published Date : 21/Oct/2010
    The Command pattern creates distance between the client that requests an operation and the object that can perform it.
    Published Date : 20/Sep/2010
    Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.
    Published Date : 22/Oct/2010
    A fully initialized instance to be copied or cloned. The Prototype pattern specifies the kind of objects to create using a prototypical instance.
    Published Date : 22/Oct/2010
There is no comments for this articles.
Leave a Reply
Display Name:
(not display in page for the security purphase)
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top