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


Top 5 Contributors of the Month
david stephan

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

 Subscribe to Articles

Observer Design Pattern.

Posted By:Krishna       Posted Date: July 15, 2013    Points: 200    Category: Architecture/Pattern    URL: http://www.dotnetspark.com  

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

Introduction :

 

Hi here I want to explain observer design pattern with simple example.


Objective: 

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

Explanation: 


When to use observer pattern?
In real time scenario we may come across different kinds of situation where some objects should take decision based on other(objects) state change.

we can achieve this by  using observer pattern,where all dependent objects will be added as observes to subject object,when a subject object state changes that  will be notify to all objects which are acting as observers,so observers will take appropriate decisions based on subject object state.

 Here  is very simple example where I had come up with a real time scenario,Cricket reporter object act as subject which collects cricket live match updates,where as all TV channels objects act as observers to the Cricket reporter object. Hence any updates in Cricket report server will sent a update notification to all his observers

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<t> 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<ICricketReporteSubscribers> _ObserversList = new List<ICricketReporteSubscribers>();

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

        }
    }
}

Please Find the out Put for above 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