Monday, April 9, 2018

Mediator Pattern

Mediator design pattern promotes loose coupling between objects and communicate between them with the help of mediator object. Mediator Pattern falls under behavioral pattern of GOF (Gang of Four) patterns.

When to use –

Mediator pattern provides a way to communicate between objects indirectly via mediator object. The mediator object facilitates the communication between objects. When you have large number of objects in your application, which is very hard to maintain you should consider using Mediator pattern.  Mediator pattern avoids direct communication between objects and it uses mediator object that is responsible to communicate between source and destination object.

There are so many practical and real life examples available for Mediator Pattern. In this article, I have used simple Chat application example in which so many participants can send their messages to other participants via mediator.

Major components of Mediator pattern –

Client – This class creates an instance of multiple participants and sends messages between them.
Mediator – The Mediator interface for communicating with different participants (colleagues).
ConcreteMediator – This class implements the Mediator interface and maintain participants.
Participant (Colleague) class – This class has reference of Mediator object and uses this object to communicate with other participants.

Let’s have a look on below example of Mediator pattern.

Code –
public interface IChatRoom
    void Register(Participant participant);
    void SendMessage(string from, string to, string message);

//Concrete Mediator
public class ChatRoom : IChatRoom
    private Dictionary<string, Participant> participants =
        new Dictionary<string, Participant>();
    public void Register(Participant participant)
        if (!participants.ContainsValue(participant))
            participants.Add(participant.Name, participant);
        participant.ChatRoom = this;

    public void SendMessage(string from, string to, string message)
        if (participants.Keys.Contains(to))
            Participant participant = participants[to];
            participant.Receive(from, message);
            Console.WriteLine("Participant not registered.");
//Participant class (Colleague class)
public class Participant
    public Participant(string name)
        Name = name;
    public ChatRoom ChatRoom { get; set; }
    public string Name { get; set; }
    public void Send(string to, string message)
        ChatRoom.SendMessage(Name, to, message);
    public void Receive(string from, string message)
        Console.WriteLine("{0} to {1} - '{2}'",
            from, Name, message);
//Client class (entry point)
class Program
    static void Main(string[] args)
        ChatRoom chatroom = new ChatRoom();
        List<string> people = new List<string>()
        { "Mitesh", "Sandeep", "Vikram", "Amit" };
        List<Participant> participants = new List<Participant>();
        foreach (string s in people)
            participants.Add(new Participant(s));
        foreach (Participant p in participants)

        participants.Find(x => x.Name.Equals("Mitesh")).
            Send("Sandeep", "Hey Sandeep, How are you?");
        participants.Find(x => x.Name.Equals("Vikram")).
            Send("Mitesh", "Where are you?");
        participants.Find(x => x.Name.Equals("Sandeep")).
            Send("Mitesh", "Hi Mitesh, i am good. Thanks");
        participants.Find(x => x.Name.Equals("Mitesh")).
            Send("Vikram", "I am at home");
        participants.Find(x => x.Name.Equals("Amit")).
            Send("Vikram", "Hi");


Output –

As you can see in this example, Participant class has reference to mediator object (ChatRoom). ChatRoom object knows about different participants and facilitates communication between them. Client creates various participants and register them with mediator object and sends messages between different participants. The sender participant does not have direct reference to receiver participant and it communicates via Mediator object.

You can download full code from Gist.

I hope this article helps you to know more about Mediator Design Pattern. Please leave your feedback in comments section below.

References –

See Also –

No comments:

Post a Comment