Mediator Pattern in C# – Real-Time Example (Centralized Communication)
What is Mediator Pattern?
The Mediator Pattern is a behavioral design pattern that centralizes communication between multiple objects.
Instead of objects communicating directly with each other, they communicate through a mediator.
👉 This reduces tight coupling.
Why Use Mediator Pattern?
- Reduce direct dependencies
- Simplify object communication
- Centralize interaction logic
- Improve maintainability
- Make systems scalable
Real-Time Scenario
Chat Application:
- User A sends message
- User B receives message
- User C receives message
👉 Users should not directly reference each other.
Mediator handles communication.
Real-Time Example – Chat System
Step 1: Mediator Class
public class ChatMediator
{
public void Send(string message, User user)
{
Console.WriteLine($"{user.Name}: {message}");
}
}
Step 2: User Class
public class User
{
private readonly ChatMediator _mediator;
public string Name { get; }
public User(ChatMediator mediator, string name)
{
_mediator = mediator;
Name = name;
}
public void Send(string message)
{
_mediator.Send(message, this);
}
}
Usage Example
var mediator = new ChatMediator();
var user1 = new User(mediator, "Sai");
var user2 = new User(mediator, "Kumar");
user1.Send("Hello");
user2.Send("Hi");
Output
Sai: Hello
Kumar: Hi
Key Concept
Instead of:
User A → User B
User A → User C
User B → User C
We do:
Users → Mediator → Users
👉 Centralized communication.
Diagram Understanding
User A
↓
Mediator
↑
User B
↑
User C
👉 All communication goes through mediator.
Advantages
- ✔ Loose coupling
- ✔ Centralized communication logic
- ✔ Easier maintenance
- ✔ Simplifies object interaction
- ✔ Better scalability
Disadvantages
- ✖ Mediator can become large/complex
- ✖ Single point of control
- ✖ More abstraction layer
When to Use
Use Mediator Pattern when:
- Many objects communicate frequently
- Direct dependencies become complex
- Centralized coordination is needed
- Communication rules change often
Real Project Mapping (.NET + Angular)
| Feature | Usage |
|---|---|
| Chat systems | Mediator |
| Service orchestration | Mediator |
| UI component communication | Mediator |
| Workflow engines | Mediator |
| CQRS/MediatR | Mediator |
ASP.NET Core Real Example
MediatR Library
CreateInvoiceCommand
↓
MediatR
↓
CreateInvoiceHandler
👉 Controller does not directly call service. Mediator handles routing.
Advanced Example – Order Workflow
OrderService
PaymentService
InventoryService
NotificationService
↓
Mediator
👉 Services communicate indirectly.
Mediator vs Observer
| Mediator | Observer |
|---|---|
| Centralized communication | One-to-many notifications |
| Controls interactions | Broadcasts events |
| Objects communicate indirectly | Observers react to events |
Pro Tip
Mediator Pattern works well with:
- CQRS
- MediatR
- Workflow systems
- Microservices orchestration
- Event-driven systems
Summary
Mediator Pattern helps you:
- Centralize communication
- Reduce tight coupling
- Simplify complex interactions
👉 Perfect for:
- Chat systems
- CQRS/MediatR
- Service coordination
- Workflow orchestration
Comments
Post a Comment