Home > Pattern notes > Observer Pattern

Observer Pattern


Observer Pattern

The Observer Pattern is a mechanism to  an object notify other objects of state changes without being dependent on their classes

Observer defines a one to many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. The Observer pattern has one subject and potentially many observers. Observers register with the subject, which notifies the observers when events occur.

UML for Observer Pattern

Subject:

 – It keeps track of all Observers and provides methods to add/remove observers.

Concrete Subject:

– Object needs to be observed

Observers:

– Provides method to notify event to observers

Concrete Observer:

it implements the update methode

   

Example:

The observers must create a specific delegate instance and register this delegate with the subject’s event.

    class Airlines

    {

        public delegate void UpdateHandler(Object sender);

        public event UpdateHandler UpdateEvent;

        public int ID { get; set; }

        public string Name { get; set; }

        public Airlines()

        {

        }

        public Airlines(string name)

        {

            Name = name;

        }

        public void AddProduct()

        {

            Notify();

        }

        void Notify()

        {

            if (UpdateEvent != null)

                UpdateEvent(this);

        }

    }

     class Agent1 : Airlines

    {

        public void OnUpdate(Object Airline)

        {

            Console.WriteLine("   Agent1    ID : {0}  — Nmae :{1}", ((Airlines)Airline).ID, ((Airlines)Airline).Name);

        }

    }

     class Agent2 : Airlines

    {

        public void OnUpdate(Object Airline)

        {

            Console.WriteLine("  Agent2      ID : {0}  — Nmae :{1}", ((Airlines)Airline).ID, ((Airlines)Airline).Name);

        }

    }

}

The main method:

Now we need to implement a client for testing this application. It’s where you will attach the observers to the concrete subject.

    class Program

    {

        static void Main(string[] args)

        {

            Airlines _Airlines = new Airlines("Chittu");

            Agent1 _agent1 = new Agent1();

            Agent2 _agent2 = new Agent2();

            _Airlines.UpdateEvent += new Airlines.UpdateHandler(_agent1.OnUpdate);

            _Airlines.UpdateEvent += new Airlines.UpdateHandler(_agent2.OnUpdate);

            _Airlines.AddProduct();

            Console.ReadLine();

        }

Advertisements
Categories: Pattern notes
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: