Mediator Pattern in C#: A Comprehensive Guide
The Mediator Pattern is a behavioral design pattern that facilitates communication between objects by centralizing their interactions. Instead of objects referring to each other directly, they communicate via a mediator. This reduces the dependencies between interacting objects, promoting loose coupling and enhancing maintainability.
Key Concepts
Mediator: Central interface or class that handles communication between various components (colleagues).
Colleague: Objects that communicate through the mediator instead of directly interacting with each other.
How to Use the Mediator Pattern
Define a Mediator Interface
Create an interface that declares methods for communication between colleagues.Implement the Mediator
Create a concrete class implementing the mediator interface to handle the interactions.Define Colleague Classes
These classes interact indirectly via the mediator.Integrate with the Mediator
The colleagues are aware of the mediator but not of each other, ensuring loose coupling.
Example in C#
Mediator Interface:
public interface IMediator
{
void Notify(object sender, string eventType);
}
Concrete Mediator:
public class ConcreteMediator : IMediator
{
private ComponentA _componentA;
private ComponentB _componentB;
public ConcreteMediator(ComponentA componentA, ComponentB componentB)
{
_componentA = componentA;
_componentA.SetMediator(this);
_componentB = componentB;
_componentB.SetMediator(this);
}
public void Notify(object sender, string eventType)
{
if (eventType == "EventA")
{
Console.WriteLine("Mediator reacts to EventA and triggers B.");
_componentB.DoSomething();
}
else if (eventType == "EventB")
{
Console.WriteLine("Mediator reacts to EventB and triggers A.");
_componentA.DoSomething();
}
}
}
Colleague Classes:
public abstract class BaseComponent
{
protected IMediator _mediator;
public void SetMediator(IMediator mediator)
{
_mediator = mediator;
}
}
public class ComponentA : BaseComponent
{
public void TriggerEventA()
{
Console.WriteLine("ComponentA triggers EventA.");
_mediator.Notify(this, "EventA");
}
public void DoSomething()
{
Console.WriteLine("ComponentA is doing something.");
}
}
public class ComponentB : BaseComponent
{
public void TriggerEventB()
{
Console.WriteLine("ComponentB triggers EventB.");
_mediator.Notify(this, "EventB");
}
public void DoSomething()
{
Console.WriteLine("ComponentB is doing something.");
}
}
Client Code:
var componentA = new ComponentA();
var componentB = new ComponentB();
var mediator = new ConcreteMediator(componentA, componentB);
componentA.TriggerEventA();
componentB.TriggerEventB();
Advantages of the Mediator Pattern
- Loose Coupling: Reduces dependencies between components, making the system easier to maintain and extend.
- Improved Readability: Centralizes communication logic, making it easier to understand the flow of interactions.
- Flexibility: Adding or modifying interactions only requires changes to the mediator, not individual components.
- Enhanced Testability: Individual components can be tested independently of others.
Disadvantages of the Mediator Pattern
- Complexity: The mediator itself can become a complex and bloated class if it handles too many interactions.
- Single Point of Failure: If the mediator fails, it impacts the entire communication network.
- Overhead: For simple interactions, using a mediator can add unnecessary complexity.
References
- Design Patterns: Elements of Reusable Object-Oriented Software - Gamma, Helm, Johnson, Vlissides. The book where the Mediator Pattern was first introduced as a behavioral pattern for reducing dependencies.
- MediatorPattern GitHub Repository - stevsharp. A practical example of the Mediator Pattern, demonstrating its implementation in C#.
- Microsoft Documentation: Design Patterns in .NET Explains common design patterns, including behavioral ones like the Mediator Pattern.
- Pluralsight Article on Mediator Pattern A guide discussing the usage and implementation of the Mediator Pattern.
- Refactoring.Guru: Mediator Pattern Provides an in-depth explanation of the Mediator Pattern with UML diagrams and real-world examples.
Conclusion
The Mediator Pattern is a powerful design pattern that simplifies the interactions between multiple objects by centralizing communication. It promotes loose coupling and improves maintainability, making it easier to extend and test individual components in complex systems. However, as with any pattern, careful consideration is required to avoid overcomplicating the mediator itself, which can become a bottleneck if not designed carefully.
Top comments (0)