.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Home >> Articles >> Architecture/Pattern >> Post New Resource Bookmark and Share

 Subscribe to Articles

Observer Design Pattern

Posted By:Murali Krishna       Posted Date: April 12, 2012    Points: 200    Category: Architecture/Pattern    URL: http://www.dotnetchallengers.com  

The objective for submitting this article is any developer who never worked on Observer design pattern should able to understand the behavior of observer pattern.
 

Basic Explanation:First of all i want to explain how observer pattern works,Suppose take a scenario where n-number of objects  called as observers will depend on one specific object sate to take decisions and this can be achieved by  using observer design pattern.Hence by using observer pattern we can achieve this where all dependent objects will be added as observes to subject ,when a state change for subject object will be notify to all observers so that all observers will take appropriate decisions based on subject object state.
  Here  is very simple example and i had come up with a real time scenario where Cricket reporter object act as subject object which gets all  cricket live match updates where as all TV channels act as observers to the Cricket reporter object hence any updates in Cricket report server will sent a update notification to all his observers(where observers are nothing but all TV channels).
When it comes to  code base here i am using two interfaces one refers to "ICricketReporter" any class implement this interface act as subject,where as "ICricketReporteSubscribers" interface  any class implement this interface will act as observer.
when it comes to  main class  we created one subject object nothing but "_Reporter" and four observers object like _Observer1,_Observer2,_Observer3,_Observer4 and all this are added as observers  to _Reporter(subject) object bu using List where t is nothing but ICricketReporteSubscribers,

    Hence if any state change of _Reporter object  will be notify to all its observers.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns
{
    public interface ICricketReporter //as Subject
    {
        void AddObserver(ICricketReporteSubscribers observer);
        void RemoveObserver(ICricketReporteSubscribers observer);
        void SendInformationToObservers(string CurrentMatchStatus);
    }
    public interface ICricketReporteSubscribers//as Observer
    {
         void updateCurrentMatchStatus(string CurrentMatchStatus);
    }

    #region Subject
    public class CricketReporter : ICricketReporter
    {
        List _ObserversList = new List();

        #region IVIPStatus Members
        public void AddObserver(ICricketReporteSubscribers observer)
        {
            _ObserversList.Add(observer);
        }
        public void RemoveObserver(ICricketReporteSubscribers observer)
        {
            _ObserversList.Remove(observer);
        }
        public void SendInformationToObservers(string CurrentMatchStatus)
        {
            foreach (ICricketReporteSubscribers _observer in _ObserversList)
            {
                _observer.updateCurrentMatchStatus(CurrentMatchStatus);
            }
           
        }
        #endregion
    }
    #endregion 

    #region Observers
    public class BCCTVChannel : ICricketReporteSubscribers //implement IObserver to behave like observer for Subject
    {
        public void updateCurrentMatchStatus(string CurrentMatchStatus)
        {
            Console.WriteLine("Match status @ BCCTVChannel :" + CurrentMatchStatus);
        }
    }
    public class CNNTVChannel : ICricketReporteSubscribers //implement IObserver to behave like observer for Subject
    {
        public void updateCurrentMatchStatus(string CurrentMatchStatus)
        {
            Console.WriteLine("Match status @ CNNTVChannel :" + CurrentMatchStatus);
        }
    }
    public class TV9TVChannel : ICricketReporteSubscribers //implement IObserver to behave like observer for Subject
    {
        public void updateCurrentMatchStatus(string CurrentMatchStatus)
        {
            Console.WriteLine("Match status @ TV9 TVChannel :" + CurrentMatchStatus);
        }
    }
    #endregion 

    public class SubnMainClass//main Class
    {
        public static void Main()
        {
            ICricketReporter _Reporter = new CricketReporter();

            BCCTVChannel _Observer1 = new BCCTVChannel();
            CNNTVChannel _Observer2 = new CNNTVChannel();
            TV9TVChannel _Observer3 = new TV9TVChannel();
            _Reporter.AddObserver(_Observer1);
            _Reporter.AddObserver(_Observer2);
            _Reporter.AddObserver(_Observer3);



            _Reporter.SendInformationToObservers("INDIA won toss and elected for Batting");
            Console.WriteLine("\n");
            _Reporter.SendInformationToObservers("INDIA Score 355/1 after 50 overs");
            Console.WriteLine("\n");
            _Reporter.SendInformationToObservers("INDIA WON THE MATCH");

            Console.Read();

        }
    }
}

Output:Check  the output and see the behavior of observer pattern between objects...Enjoy the code(:

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend