Click here to Skip to main content
15,894,405 members
Articles / Programming Languages / C#

Mediator Pattern

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
20 Oct 2013CPOL1 min read 12.9K   9  
The mediator pattern encapsulate the interaction between a set of objects. Also it encapsulate the protocol between those objects. The pattern help 

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Introduction

The mediator pattern encapsulate the interaction between a set of objects. Also it encapsulate the protocol between those objects. The pattern help  to loosely couple the object by keeping them from referring one each other.

Use Cases for the Mediator Pattern

You should use the pattern in the following cases:

  • Behaviour that is distributed between some objects can be grouped or customized.
  • Object reuse is difficult because it communicates with other objects.
  • Objects in the system communicate in well-defined but complex ways.

UML Diagram

Image 1

Example in C#

The following code is an example of how to implement the pattern:

C#
#region Mediator

public interface IMediator
{
    #region Methods
    void Send(string message, Colleague colleague);
    #endregion
}

#endregion

#region Concrete Mediator

public class ConcreteMediator : IMediator
{
    #region Properties
    public List<ConcreteColleague> Colleagues { get; private set; }
    #endregion

    #region Ctor
    public ConcreteMediator()
    {
        Colleagues = new List<ConcreteColleague>();
    }
    #endregion

    #region Mediator Members
    public void Send(string message, Colleague colleague)
    {
        foreach (Colleague currentColleague in Colleagues)
        {
            if (!currentColleague.Equals(colleague))
            {
                currentColleague.Recieve(message);
            }
        }
    }
    #endregion
}

#endregion

#region Colleague

public abstract class Colleague
{
    #region Members
    protected IMediator _mediator;
    #endregion

    #region Ctor
    public Colleague(IMediator mediator)
    {
        _mediator = mediator;
    }
    #endregion

    #region Methods
    /// <summary>
    /// Sends the given message
    /// </summary>
    /// <param name="message">The given message</param>
    public abstract void Send(string message);

    /// <summary>
    /// Recieves the given message
    /// </summary>
    /// <param name="message">The given message</param>
    public abstract void Recieve(string message);
    #endregion
}

#endregion

#region Concrete Colleague

public class ConcreteColleague : Colleague
{
    #region Properties
    public int ID { get; set; }
    #endregion

    #region Ctor
    public ConcreteColleague(IMediator mediator, int id)
        : base(mediator)
    {
        ID = id;
    }
    #endregion

    #region Methods
    public override void Send(string message)
    {
        _mediator.Send(message, this);
    }

    public override void Recieve(string message)
    {
        Console.WriteLine("{0} recieved the message: {1}", ID, message);
    }
    #endregion
}

#endregion

As can be seen in the example, I have a mediator object that sends messages to the concretes of the colleague class. The example is simple but it shows the concepts that are used in the mediator pattern. With few modifications you can use the example to build a small chat room application.

The following code was used to test the previous example:

C#
ConcreteMediator mediator = new ConcreteMediator();
ConcreteColleague colleague1 = new ConcreteColleague(mediator, 1);
ConcreteColleague colleague2 = new ConcreteColleague(mediator, 2);

mediator.Colleagues.Add(colleague1);
mediator.Colleagues.Add(colleague2);

colleague1.Send("Hello from colleague 1");
colleague2.Send("Hello from colleague 2");

Console.Read();

Summary

To sum up, I explained the use of the mediator pattern.
When considering to use the mediator pattern you should always remember that the patten has a performance impact. The performance impact is caused because every communications pass through the mediator on the way to the communicated object – which make the mediator a bottle neck in your system.
If you don’t want to use the pattern I suggest the façade pattern instead to wrap your subsystem.

This article was originally posted at http://wiki.asp.net/page.aspx/494/mediator-pattern

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --