What is Mediator Pattern?
The Mediator Pattern is a behavioral design pattern where a central object controls communication between multiple objects.
Instead of objects talking to each other directly, they communicate through a mediator.
Why Use Mediator Pattern?
- Avoid complex object-to-object dependencies
- Centralize communication logic
- Improve maintainability and scalability
- Reduce tight coupling
Real-Time Scenario
- Chat system (users send messages via server)
- Multiple services interacting in backend
- Form components interacting in UI
- Microservices coordination
Basic Implementation
Mediator
public class Mediator
{
public void Send(string message, User user)
{
Console.WriteLine($"{user.Name}: {message}");
}
}
Colleague (User)
public class User
{
private readonly Mediator _mediator;
public string Name { get; }
public User(Mediator mediator, string name)
{
_mediator = mediator;
Name = name;
}
public void Send(string message)
{
_mediator.Send(message, this);
}
}
Usage Example
var mediator = new Mediator();
var user1 = new User(mediator, "Alice");
var user2 = new User(mediator, "Bob");
user1.Send("Hello");
user2.Send("Hi there");
Improved Real-World Example (.NET Service Coordination)
Services
public class OrderService
{
public void CreateOrder() => Console.WriteLine("Order Created");
}
public class PaymentService
{
public void ProcessPayment() => Console.WriteLine("Payment Done");
}
public class NotificationService
{
public void Send() => Console.WriteLine("Notification Sent");
}
Mediator
public class OrderMediator
{
private readonly OrderService _order;
private readonly PaymentService _payment;
private readonly NotificationService _notification;
public OrderMediator(OrderService order, PaymentService payment, NotificationService notification)
{
_order = order;
_payment = payment;
_notification = notification;
}
public void PlaceOrder()
{
_order.CreateOrder();
_payment.ProcessPayment();
_notification.Send();
}
}
Usage
var mediator = new OrderMediator(
new OrderService(),
new PaymentService(),
new NotificationService()
);
mediator.PlaceOrder();
Key Concept
Instead of:
orderService -> paymentService -> notificationService
We do:
mediator -> controls everything
👉 Communication is centralized
Pros
- Loose coupling between objects
- Flexible and scalable behavior
- Easier to extend features
- Better separation of concerns
Cons
- More classes and complexity
- Harder debugging
- Learning curve
When to Use
- When multiple objects need to communicate
- When behavior needs to change dynamically
- When reducing direct dependency is important
- When implementing event-driven systems
Real Project Mapping (.NET + Angular)
| Feature | Pattern |
|---|---|
| Service coordination | Mediator |
| CQRS (MediatR) | Mediator |
| Chat systems | Mediator |
| Complex workflows | Mediator |
Pro Tip (Advanced .NET Usage)
Use MediatR library:
- Each request → handled by mediator
- Clean CQRS implementation
- Decouples controllers from services
Example:
public record CreateInvoiceCommand(string Customer, decimal Amount);
Summary
Mediator Pattern helps you:
- Centralize communication
- Reduce dependencies
- Build scalable and maintainable systems
👉 Perfect for CQRS, microservices, workflows, complex service interactions
Comments
Post a Comment