What Is the Mediator Design Pattern and When to Use It in C#?

What Is the Mediator Design Pattern and When to Use It in C#?

The Mediator design pattern is a behavioral pattern that allows objects to communicate with each other without knowing the identities of their peers. Instead, objects communicate through a mediator object, which acts as an intermediary between them.

In C#, the Mediator pattern can be implemented by creating a mediator interface that defines the methods that the objects will use to communicate, and a concrete mediator class that implements the interface. The objects that need to communicate with each other will also need to implement the same interface.

In the following example, we will learn how to implement the Mediator pattern in C#:

interface IMediator
{
    void Send(string message, Colleague colleague);
}

abstract class Colleague
{
    protected IMediator mediator;

    public Colleague(IMediator mediator)
    {
        this.mediator = mediator;
    }
}

class ConcreteColleagueA : Colleague
{
    public ConcreteColleagueA(IMediator mediator) : base(mediator) { }

    public void Send(string message)
    {
        mediator.Send(message, this);
    }

    public void Notify(string message)
    {
        Console.WriteLine("ColleagueA gets message: " + message);
    }
}

class ConcreteColleagueB : Colleague
{
    public ConcreteColleagueB(IMediator mediator) : base(mediator) { }

    public void Send(string message)
    {
        mediator.Send(message, this);
    }

    public void Notify(string message)
    {
        Console.WriteLine("ColleagueB gets message: " + message);
    }
}

class ConcreteMediator : IMediator
{
    private ConcreteColleagueA colleagueA;
    private ConcreteColleagueB colleagueB;

    public ConcreteColleagueA ColleagueA
    {
        set { colleagueA = value; }
    }

    public ConcreteColleagueB ColleagueB
    {
        set { colleagueB = value; }
    }

    public void Send(string message, Colleague colleague)
    {
        if (colleague == colleagueA)
        {
            colleagueB.Notify(message);
        }
        else
        {
            colleagueA.Notify(message);
        }
    }
}

This is just one way to implement the Mediator pattern in C#, and you can modify it to suit your needs. Here, IMediator is an interface that defines a single method Send(string message, Colleague colleague) which is used to send messages between colleagues. Colleague is an abstract class that has a reference to a mediator and ConcreteColleagueA and ConcreteColleagueB are concrete classes that implement the Colleague class. ConcreteMediator is the actual mediator that receives messages from colleagues and redirects them to the appropriate colleague.

One of the main benefits of using the Mediator pattern is that it promotes loose coupling between objects by removing the need for them to know about each other's existence. This makes the code more flexible and easier to maintain.

An example of when the Mediator pattern could be used is in a chat application. In this case, a mediator class could be used to handle the communication between different users. The mediator class would have methods for sending and receiving messages, and the user objects would use these methods to communicate with each other without knowing the identities of their peers.

Another example could be a system that controls the interaction between different components of an aircraft, such as the engines, the fuel system, and the navigation system. The mediator class would handle the communication between the different components, and ensure that they are working together in a safe and efficient manner.

In summary, the Mediator pattern is a powerful pattern for managing communication between objects in C#. It promotes loose coupling and makes the code more flexible and easier to maintain. It can be useful in situations where multiple objects need to interact with each other, but should not be tightly coupled to one another.

Post a Comment

Previous Post Next Post