DEV Community

Juarez Júnior for Develop4Us

Posted on • Edited on

C# Design Pattern: Mediator

The Mediator pattern is used to reduce direct dependencies between objects by introducing a mediator object that controls communication between them. This is useful when you have many objects that need to communicate, but you don’t want them to be tightly coupled to each other. The Mediator centralizes this communication, making the code more flexible and easier to maintain.

C# Code Example:

// Mediator interface
public interface IMediator
{
    void SendMessage(string message, Colleague colleague);
}

// Base class for colleagues that communicate through the mediator
public abstract class Colleague
{
    protected IMediator _mediator;

    public Colleague(IMediator mediator)
    {
        _mediator = mediator;
    }

    public abstract void ReceiveMessage(string message);
}

// Concrete Colleague 1
public class ConcreteColleague1 : Colleague
{
    public ConcreteColleague1(IMediator mediator) : base(mediator) { }

    public void Send(string message)
    {
        Console.WriteLine($"Colleague 1 sending message: {message}");
        _mediator.SendMessage(message, this);
    }

    public override void ReceiveMessage(string message)
    {
        Console.WriteLine($"Colleague 1 received message: {message}");
    }
}

// Concrete Colleague 2
public class ConcreteColleague2 : Colleague
{
    public ConcreteColleague2(IMediator mediator) : base(mediator) { }

    public void Send(string message)
    {
        Console.WriteLine($"Colleague 2 sending message: {message}");
        _mediator.SendMessage(message, this);
    }

    public override void ReceiveMessage(string message)
    {
        Console.WriteLine($"Colleague 2 received message: {message}");
    }
}

// Concrete Mediator managing the communication
public class ConcreteMediator : IMediator
{
    private ConcreteColleague1 _colleague1;
    private ConcreteColleague2 _colleague2;

    public ConcreteColleague1 Colleague1
    {
        set { _colleague1 = value; }
    }

    public ConcreteColleague2 Colleague2
    {
        set { _colleague2 = value; }
    }

    public void SendMessage(string message, Colleague colleague)
    {
        if (colleague == _colleague1)
        {
            _colleague2.ReceiveMessage(message);
        }
        else
        {
            _colleague1.ReceiveMessage(message);
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Create the mediator
        ConcreteMediator mediator = new ConcreteMediator();

        // Create colleagues
        ConcreteColleague1 colleague1 = new ConcreteColleague1(mediator);
        ConcreteColleague2 colleague2 = new ConcreteColleague2(mediator);

        // Set colleagues in the mediator
        mediator.Colleague1 = colleague1;
        mediator.Colleague2 = colleague2;

        // Colleague 1 sends a message
        colleague1.Send("Hello from Colleague 1");

        // Colleague 2 sends a message
        colleague2.Send("Hi from Colleague 2");
    }
}
Enter fullscreen mode Exit fullscreen mode

Code Explanation:

In this example, the ConcreteMediator manages communication between two colleagues, ConcreteColleague1 and ConcreteColleague2. When one colleague sends a message, the mediator decides which colleague to send the message to. This reduces direct coupling between the colleagues since they don’t need to know about each other, only the mediator.

Conclusion:

The Mediator pattern is useful when you want to centralize communication between multiple objects, preventing them from being directly coupled. This makes the code more modular and easier to modify, as you can change the communication logic without altering the individual objects.

Source code: GitHub

Top comments (0)