Home
Gallery
GuestBook
SourceCode
Contact

Design Patterns for Dummies. The Observer Pattern

Posted in Design Patterns
This post has been read 3285 times

In continuation with the series on design pattern, I am currently going through Behavioral Patterns. Today I will write about the Observer Pattern. You can read about rest of the patterns from the following links

  1. Strategy Pattern
  2. State Pattern
  3. Template Method Pattern
  4. Chain of Responsibility Pattern
  5. Command Pattern
  6. Iterator Pattern
  7. Mediator Pattern

You can read about the Structural patterns from here.
You can read about the Creational patterns from here.

The Observer pattern deals with providing a relation between objects such that when one objects changes its state, then all the other objects that have a relation to it are notified of the change. This could be achieved by using a single publisher of the state of object and many subscribers who may want the notification for the changed state.

The Observer is usually composed of 2 class viz. a Subject whose state may change periodically and the Observer itself who maybe notified of the change, if they wish to receive the state change information.

The following example is based on the example from Dofactory website

    1 namespace PatternsConsole

    2 {

    3     class ObserverPattern

    4     {

    5         public delegate void ChangeEventHandler<TArg1, TArg2>( TArg1 sender, TArg2 eventArgs );

    6 

    7         public class ChangeEventArgs : EventArgs

    8         {

    9             public string Title { get; set; }

   10             public string Name { get; set; }

   11 

   12             public ChangeEventArgs( string name, string title )

   13             {

   14                 Name = name;

   15                 Title = title;

   16             }

   17 

   18         }

   19 

   20         // Subject

   21         class Blog

   22         {

   23             public string Name { get; private set; }

   24             protected string title;

   25 

   26             // Constructor

   27             public Blog( string name, string title )

   28             {

   29                 Name = name;

   30                 this.title = title;

   31             }

   32 

   33             public event ChangeEventHandler<Blog, ChangeEventArgs> Change;

   34 

   35             public virtual void OnChange( ChangeEventArgs e )

   36             {

   37                 if ( Change != null )

   38                 {

   39                     Change( this, e );

   40                 }

   41             }

   42 

   43             public void Attach( IObserver observer )

   44             {

   45                 Change += observer.Update;

   46             }

   47 

   48             public string Title

   49             {

   50                 set

   51                 {

   52                     title = value;

   53                     OnChange( new ChangeEventArgs( Name, title ) );

   54                     Console.WriteLine( "" );

   55                 }

   56             }

   57         }

   58 

   59         // Observer

   60         interface IObserver

   61         {

   62             void Update( Blog sender, ChangeEventArgs e );

   63         }

   64 

   65         // ConcreteObserver

   66         class Observer : IObserver

   67         {

   68             public string Name { get; private set; }

   69 

   70             public Observer( string name )

   71             {

   72                 Name = name;

   73             }

   74 

   75             public void Update( Blog sender, ChangeEventArgs e )

   76             {

   77                 Console.WriteLine( "Notified {0} of {1}'s " + "post : {2}", Name, e.Name, e.Title );

   78             }

   79         }

   80 

   81         static void Main()

   82         {

   83             var rob = new Blog( "Rob Conery", "Weke Road" );

   84             rob.Attach( new Observer( "Vinay" ) );

   85 

   86             rob.Title = "Subsonic 3";

   87             rob.Title = "Storefront Finis";

   88             rob.Title = "Oxite Refactor";

   89 

   90             //Output :

   91             // Notified Vinay of Rob Conery's post : Subsonic 3

   92             //

   93             // Notified Vinay of Rob Conery's post : Storefront Finis

   94             //

   95             // Notified Vinay of Rob Conery's post : Oxite Refactor

   96 

   97             Console.ReadLine();

   98         }

   99     }

  100 }

We can use the observer pattern when changes in one object needs to be propagated to other objects or when the object sending the changes does not need to know about the receivers.

In my next post I will be writing about the Visitor Pattern




Comment posted on Thursday, November 19, 2009 1:40 PM
Hi vinay,i understand above every thing about the desing patterns,
but pls explain me/other how to and where ,genery we where use these patterns in gui,businee layer,dal,

hope this wil help for thousend of people
Name:
E-mail:
Website:
Comment:
 
Anti Bot Image:

Insert Cancel


Subscribe

Random Photo

My Tweets


Top Posts

Source Code

The source code to this site is open-source. You can download the code from here.

Categories


Recent Blogs


Archives


Blogroll