SIGN UP MEMBER LOGIN:    
ARTICLE

Observer Design Pattern

Posted by Srihari Chinna Articles | Design & Architecture September 20, 2010
The Observer pattern defines a one to many relationships between objects so that when one changes its state, all the others are notified accordingly.
Reader Level:


Definition

A way of notifying dependent classes of changes.

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.

Design

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 so the Subject/Target will send them notifications.



Code

//Notifier

    public delegate void ChangedEventHandler(object sender, ChangedEventArgs e);

    //Subject

    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());

        }
    }
//Client
static void Main(string[] args)
        {
            Target target = new Target();

            ObserverOne observer1 = new ObserverOne(target);
            ObserverTwo observer2 = new ObserverTwo(target);

            target.Add("Chinna");
        }
 

Login to add your contents and source code to this article
share this article :
post comment
 

U are right... For now I removed the UML diagram..I will upload the new one later... Chinna

Posted by Srihari Chinna Dec 02, 2011

Shouldn't that empty diamond in uml diagram be on subject entity instead os observer??

Posted by jan teren Dec 02, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor