BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Design Patterns
.NET 2.0+

Mediator Design Pattern

The mediator pattern is a design pattern that promotes loose coupling of objects by removing the need for classes to communicate with each other directly. Instead, mediator objects are used to encapsulate and centralise the interactions between classes.

Example Mediator

In the opening section of the article an example mediator was described. In this example the colleagues are a presenter, who is giving a demonstration, and a variable number of attendees, who are watching the presentation. The mediator holds references to a single presenter and multiple attendee objects with the latter held in a list. The attendees may ask questions. The presenter may answer questions and send new images to the attendees.

The code for the example is as follows. You should create this in a console application as the actions are outputted to the console. Each communication's output is shown using a different console colour for readability.

public abstract class PresentationMember
{
    protected Mediator _mediator;

    public PresentationMember(Mediator mediator)
    {
        _mediator = mediator;
    }

    public string Name { get; set; }

    public void ReceiveAnswer(string answer)
    {
        Console.ForegroundColor = ConsoleColor.Cyan;
        Console.WriteLine("{0} received answer.\n'{1}'.", Name, answer);
    }
}


public class Presenter : PresentationMember
{
    public Presenter(Mediator mediator) : base(mediator) { }

    public void SendNewImageUrl(string url)
    {
        Console.ForegroundColor = ConsoleColor.White;
        Console.WriteLine("Presenter changed image URL to '{0}'.", url);
        _mediator.UpdateImage(url);
    }

    public void ReceiveQuestion(string question, Attendee attendee)
    {
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("Presenter received question from {0}.\n'{1}'"
            , attendee.Name, question);
    }

    public void AnswerQuestion(string answer, Attendee attendee)
    {
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine("Presenter answered question for {0}.\n'{1}'"
            , attendee.Name, answer);
        _mediator.SendAnswer(answer, attendee);
    }
}


public class Attendee : PresentationMember
{
    public Attendee(Mediator mediator) : base(mediator) { }

    public void AskQuestion(string question)
    {
        Console.ForegroundColor = ConsoleColor.Red;
        Console.WriteLine("{0} asked question.\n'{1}'", Name, question);
        _mediator.SendQuestion(question, this);
    }

    public void ReceiveImage(string url)
    {
        Console.ForegroundColor = ConsoleColor.Yellow;
        Console.WriteLine("Image for {0} updated to '{1}'.", Name, url);
    }
}


public class Mediator
{
    public Presenter Presenter { get; set; }

    public List<Attendee> Attendees { get; set; }

    public void UpdateImage(string url)
    {
        foreach (Attendee attendee in Attendees)
        {
            attendee.ReceiveImage(url);
        }
    }

    public void SendAnswer(string answer, Attendee attendee)
    {
        attendee.ReceiveAnswer(answer);
    }

    public void SendQuestion(string question, Attendee attendee)
    {
        Presenter.ReceiveQuestion(question, attendee);
    }
}

Testing the Mediator

To test the mediator we can use each of the methods available to the presenter and the attendees. Add the following code to the Main method of the program and execute it to see the results.

Mediator mediator = new Mediator();

Presenter presenter = new Presenter(mediator);
presenter.Name = "Bob";
mediator.Presenter = presenter;

Attendee sam = new Attendee(mediator);
sam.Name = "Sam";
Attendee jim = new Attendee(mediator);
jim.Name = "Jim";
mediator.Attendees = new List<Attendee> { sam, jim };

presenter.SendNewImageUrl("Slide1.jpg");
sam.AskQuestion("How often should I do this?");
presenter.AnswerQuestion("Daily", sam);
presenter.SendNewImageUrl("Slide2.jpg");

/* OUTPUT

Presenter changed image URL to 'Slide1.jpg'.
Image for Sam updated to 'Slide1.jpg'.
Image for Jim updated to 'Slide1.jpg'.
Sam asked question.
'How often should I do this?'
Presenter received question from Sam.
'How often should I do this?'
Presenter answered question for Sam.
'Daily'
Sam received answer.
'Daily'.
Presenter changed image URL to 'Slide2.jpg'.
Image for Sam updated to 'Slide2.jpg'.
Image for Jim updated to 'Slide2.jpg'.

*/
30 July 2009