Home
Gallery
GuestBook
SourceCode
Contact

Design Patterns for Dummies. The Mediator Pattern

Posted in Design Patterns
This post has been read 3196 times

In continuation with the series on design pattern, I am currently going through Behavioral Patterns. Today I will write about the Mediator 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

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

The Mediator patterns deals with providing a way for objects to communicate with each other without knowing each others identity. The participants involved in this pattern are a Mediator and Colleague. A concrete colleague is used to communicate with other colleagues through its mediator.

    1 namespace PatternsConsole

    2 {

    3     class MediatorPattern

    4     {

    5         public delegate void Callback( string message, string from );

    6 

    7         class Mediator

    8         {

    9             Callback respond;

   10 

   11             public void Register( Callback method )

   12             {

   13                 respond += method;

   14             }

   15 

   16             // Send is implemented as a broadcast

   17             public void Send( string message, string from )

   18             {

   19                 respond( message, from );

   20                 Console.WriteLine();

   21             }

   22         }

   23 

   24         class FooColleague

   25         {

   26             Mediator mediator;

   27             protected string name;

   28 

   29             public FooColleague( Mediator mediator, string name )

   30             {

   31                 this.mediator = mediator;

   32                 this.name = name;

   33                 mediator.Register( Receive );

   34             }

   35 

   36             public virtual void Receive( string message, string from )

   37             {

   38                 if ( !string.Equals( from, name ) )

   39                     Console.WriteLine( name + " received from " + from + ": " + message );

   40             }

   41 

   42             public void Send( string message )

   43             {

   44                 Console.WriteLine( "Send (From " + name + "): ");

   45                 mediator.Send( message, name );

   46             }

   47 

   48         }

   49 

   50         class BarColleague : FooColleague

   51         {

   52             public BarColleague( Mediator mediator, string name )

   53                 : base( mediator, name ) { }

   54 

   55             public override void Receive( string message, string from )

   56             {

   57                 if ( !string.Equals( from, name ) )

   58                     Console.WriteLine( name + " received from " + from + ": " + message );

   59             }

   60         }

   61 

   62         static void Main()

   63         {

   64             var mediator = new Mediator();

   65 

   66             var foo = new FooColleague( mediator, "Foo" );

   67             var bar = new BarColleague( mediator, "Bar" );

   68 

   69             foo.Send( "Hi Bar! This is a greeting through the mediator" );

   70             bar.Send( "Hi Foo! Really!!" );

   71 

   72             // Output :

   73             // Send (From Foo):

   74             // Bar received from Foo: Hi Bar! This is a greeting through the mediator

   75 

   76             // Send (From Bar):

   77             // Foo received from Bar: Hi Foo! Really!!

   78 

   79             Console.ReadLine();

   80         }

   81     }

   82 }

We can use the mediator pattern when we have objects are communicating in a complex way or when we have to protect the identities of objects who are communicating.

In my next post I will be writing about the Observer Pattern.




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