Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Behavioral Patterns. Observer Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Behavioral Patterns. Observer
SoftPilot.2000 - Software Outsourcing Ukraine. Articles. Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Behavioral Patterns. Observer
SoftPilot.2000 - Software Outsourcing Ukraine. Articles. Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Behavioral Patterns. Observer
SoftPilot.2000 - Software Outsourcing Ukraine. Articles. Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Behavioral Patterns. Observer
Software Outsourcing Ukraine : softPilot.2000 - Site Map Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Behavioral Patterns. Observer
Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Behavioral Patterns. Observer - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Behavioral Patterns. Observer - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Behavioral Patterns. Observer - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Behavioral Patterns. Observer - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Behavioral Patterns. Observer - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Behavioral Patterns. Observer - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services Software Outsourcing Ukraine. Design Patterns. C#, dotNet. Behavioral Patterns. Observer - Offshore Programming : professional dotNet B2B solutions : C#, Smart Client, Windows Forms, AJAX, Web Services
SoftPilot.2000 : Software Outsourcing Ukraine- Home SoftPilot.2000 : Software Outsourcing Ukraine - About SoftPilot.2000 : Software Outsourcing Ukraine - Company Profile Software Development Ukraine : SoftPilot.2000 - Case Study SoftPilot.2000 : Software Outsourcing Ukraine - Services SoftPilot.2000 : Software Outsourcing Ukraine - Partners SoftPilot.2000 : Software Outsourcing Ukraine - Contacts SoftPilot.2000 : Software Outsourcing Ukraine - E-mail SoftPilot.2000 : Software Outsourcing to Ukraine

SoftPilot.2000 - Software Outsourcing to Ukraine.

Software development articles about design patterns implementation in C#.

Behavioral Patterns.




Design Pattern Observer

Really useful design pattern. Often we have several object hierarchies which should notify each others about object state changes. Design Pattern Observer is standard template for implementation in this case.

For example, we have the "Birth Day" object which derived from the "Event". Also, we have a hierarchy of the "Receivers" which subscribed to the "Birth Day" event. If the "Description" of the "Birth Day" changes, all subscribed receivers will be notified.

In turn to implement such behavior we should, during the "Description" changing, to have the list of subscribers in the "Birth Day" object and call the method which is responsible for the notification for each subscriber.

As an enhancement, we could also implement the mechanism for getting state of the "ConcreteSubject" or "BirthDay" object by adding the GetState() method and "State" field.

Let's try to implement this in C#.

 

   
  Observer Design Pattern
C# implementation:

//------------------------------------
// Observer design pattern implementation in C#
//------------------------------------
using System;
using System.Collections;

// "Subject"
abstract class Event
{
// Fields
protected string description;
private ArrayList receivers = new ArrayList();

// Constructor
public Event( string d ) {this.description = d; }

// Methods
public void Attach( Receiver receiver ) { receivers.Add( receiver ); }
public void Detach( Receiver receiver ) { receivers.Remove( receiver ); }
public void Notify()
{
  foreach( Receiver r in receivers )
    r.Notify( this );
}

// Properties
public string Description
{
  get{ return description; }
  set
  {
    description = value;
    Notify(); }
  }
}

//--------------------------------------------
// "ConcreteSubject"
class BirthDay : Event
{
// Constructor
public BirthDay( string descr )
  : base( descr )
    {}
}
//--------------------------------------------

// "Observer"
interface IReceiver
{
// Methods
void Notify( Event e );
}

//--------------------------------------------
// "ConcreteObserver"

class Receiver : IReceiver
{
// Fields
  private string name;
  private Event e;

// Constructors
public Receiver( string name ) { this.name = name; }

// Methods
public void Notify( Event e )
{
  Console.WriteLine( "Notified receiver {0} about {1} ", name, e.Description );
}

// Properties
public Event Event
{
  get{ return e; }
  set{ e = value; }
}
}
//--------------------------------------------

public class ObserverTest
{
public static void Main( string[] args )
{
// Create list of receivers
  Receiver r1 = new Receiver( "Jerry" );
  Receiver r2 = new Receiver( "Tom" );

// Create "Birth Day" event and attach the receivers
  BirthDay b1 = new BirthDay( "Bill's birth day." );
  b1.Attach( r1 );
  b1.Attach( r2 );

// Change "Birth Day" description, which notifies receivers
  b1.Description = "Sam's birth day.";
  b1.Description = "Mom's birth day.";

  Console.Read();
}
}

Design Patterns. Observer. C#.



The results are below:


Notified receiver Jerry about Sam's birth day.
Notified receiver Tom about Sam's birth day.
Notified receiver Jerry about Mom's birth day.
Notified receiver Tom about Mom's birth day.



 
   
(C) SoftPilot.2000 All Rights Reserved. 2000 - 2006