Mediator Pattern in C#: From Basics to Advanced

Laks Tutor
3 min readAug 17, 2023

--

The Mediator Pattern is a behavioral design pattern that promotes loose coupling by ensuring that instead of components communicating directly with each other, they communicate through a mediator. This pattern is particularly useful for ensuring that individual components can be reused and tested independently. In this blog post, we’ll explore the Mediator Pattern in C#, starting from the basics and progressing to advanced scenarios.

Basics of Mediator Pattern

Concept

The Mediator Pattern aims to reduce connections between multiple classes or objects by centralizing external communications. By doing so, it helps in reducing the dependencies between classes, leading to a decrease in the complexity of the system.

Participants

  1. Mediator: Defines the interface for communication between Colleague objects.
  2. ConcreteMediator: Implements the Mediator interface and coordinates communication between Colleague objects.
  3. Colleague: Defines the interface for communication with other Colleague objects through its Mediator.
  4. ConcreteColleague: Implements the Colleague interface and communicates with other colleagues through its Mediator.

Simple Implementation

Let’s start with a basic example: a chat room.

// Mediator
public interface IChatMediator
{
void SendMessage(string message, Participant participant);
void Register(Participant participant);
}

// ConcreteMediator
public class ChatRoom : IChatMediator
{
private List<Participant> _participants = new List<Participant>();
public void Register(Participant participant)
{
_participants.Add(participant);
}
public void SendMessage(string message, Participant sender)
{
foreach (var participant in _participants)
{
if (participant != sender)
{
participant.Receive(message);
}
}
}
}
// Colleague
public abstract class Participant
{
protected IChatMediator _mediator;
public Participant(IChatMediator mediator)
{
_mediator = mediator;
}
public abstract void Send(string message);
public abstract void Receive(string message);
}
// ConcreteColleague
public class User : Participant
{
public User(IChatMediator mediator) : base(mediator) { }
public override void Send(string message)
{
_mediator.SendMessage(message, this);
}
public override void Receive(string message)
{
Console.WriteLine($"User received: {message}");
}
}

Advanced Usage of Mediator Pattern

1. Event Handling

The Mediator Pattern can be used to handle events in a centralized manner. For instance, in a GUI, where multiple components might interact based on various events, a mediator can help in managing these interactions.

2. Dynamic Mediators

In some scenarios, the mediator’s behavior might need to change at runtime. This can be achieved by combining the Mediator Pattern with the Strategy Pattern.

3. Mediator and Observer

The Mediator Pattern can be combined with the Observer Pattern to create a more dynamic and flexible system. The mediator can act as an observer, being notified of changes in its colleagues and acting accordingly.

4. Limiting Mediator Responsibilities

It’s essential to ensure that the mediator doesn’t become a “god object” with too many responsibilities. The mediator should only handle communication between colleagues and not hold business logic.

5. Mediator in .NET Framework

.NET Framework uses the Mediator Pattern in various places. One of the most notable examples is the Windows Presentation Foundation (WPF) where the Binding class acts as a mediator between UI components and data sources.

Conclusion

The Mediator Pattern is a powerful design pattern that promotes loose coupling and enhances the reusability and testability of components. By centralizing communication, it simplifies system complexity and promotes a single point of communication, making the system easier to understand and maintain. Whether you’re building a complex GUI application or designing a system with multiple interacting components, the Mediator Pattern can be an invaluable tool in your design arsenal.

--

--

Laks Tutor

Software Architect & .NET expert. Specializing in Docker & Kubernetes. Freelance corporate trainer. Shaping tech & sharing insights on Medium.