Design Patterns: Mediator

The mediator pattern allows objects to communicate to each other through a common instance of a mediator class. It promotes loose coupling and prevent objects to refer to each other directly.

Through a common instance of a mediator the classes subscribing to it can communicate by sending messages to this instance and it will notify the other subscribed instances.

2015-39-09 12-39-15-719

public abstract class Mediator
{
    public IList<AirCraft> AirCrafts { get; private set; }

    public Mediator()
    {
        AirCrafts = new List<AirCraft>();
    }

    public abstract void Send(AirCraft sender, string message);
}

public class CommunicationTower : Mediator
{
    public override void Send(AirCraft sender, string message)
    {
        foreach (var airCraft in AirCrafts)
        {
            if (airCraft != sender)
            {
                airCraft.Receive(sender, message);
            }
        }
    }
}

public abstract class AirCraftCollegue
{
    public abstract void Receive(AirCraft sender, string message);
}

public class AirCraft : AirCraftCollegue
{
    public AirCraft(CommunicationTower mediator)
    {
        mediator.AirCrafts.Add(this);
    }

    public string Name { get; set; }

    public override void Receive(AirCraft sender, string message)
    {
        Console.WriteLine("{0}: Received message '{1}' from '{2}'"
            , Name, message, sender.Name);
    }
}

In the above example the mediator tells the subscribed aircrafts (except the sender) that one of them is saying something.

Example usage:

class Program
{
    static void Main(string[] args)
    {
        var towerMediator = new CommunicationTower();

        var airCraft1 = new AirCraft(towerMediator) { Name = "Unit #1" };
        var airCraft2 = new AirCraft(towerMediator) { Name = "Unit #2" };
        var airCraft3 = new AirCraft(towerMediator) { Name = "Unit #3" };
        var airCraft4 = new AirCraft(towerMediator) { Name = "Unit #4" };
        var airCraft5 = new AirCraft(towerMediator) { Name = "Unit #5" };

        towerMediator.Send(airCraft1, "Let's go up!");
    }
}

Output:

Unit #2: Received message 'Let's go up!' from 'Unit #1'
Unit #3: Received message 'Let's go up!' from 'Unit #1'
Unit #4: Received message 'Let's go up!' from 'Unit #1'
Unit #5: Received message 'Let's go up!' from 'Unit #1'

One real world example is the EventAggreagator that we see on WPF. It allows the communication between view models that don’t know about the existence of each other.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s