Ever been to a party where the host (or party planner) made sure everyone was mingling and having a good time, without you having to worry about calling out names or coordinating conversations? That’s exactly what the Mediator Pattern does in your code. It centralizes communication between objects, letting them interact indirectly via a mediator—just like a savvy party planner who keeps the bash buzzing smoothly.
Why Bother with It?
- Centralized Communication: Instead of having each guest (object) talk directly to every other guest, the party planner (mediator) handles all the interactions. This reduces the number of connections and keeps things organized.
- Decoupled Design: Guests don’t need to know about each other’s business. They just interact with the planner, making your code less tangled and easier to maintain.
- Flexible and Scalable: Adding new guests to the party is a breeze—the party planner adjusts without requiring changes from every guest. Your system can evolve smoothly without rewriting every interaction.
How It Works (In Plain English)
- The Party Planner (Mediator): Acts as the central hub that coordinates interactions. When a guest wants to share something, they tell the planner, who then relays the message to the appropriate guests.
- The Guests (Colleagues): Each guest represents a component in your system. They communicate by sending messages to the planner rather than directly with each other.
- Indirect Communication: The party planner ensures that messages reach the right people without each guest needing to know all the details about the other guests.
C# Code Example
using System;
using System.Collections.Generic;
// The Mediator interface defines how messages are sent.
public interface IPartyMediator
{
void SendMessage(string message, PartyParticipant sender);
void Register(PartyParticipant participant);
}
// The Concrete Mediator: our friendly party planner.
public class PartyPlanner : IPartyMediator
{
private readonly List<PartyParticipant> _participants = new List<PartyParticipant>();
public void Register(PartyParticipant participant)
{
_participants.Add(participant);
}
public void SendMessage(string message, PartyParticipant sender)
{
foreach (var participant in _participants)
{
if (participant != sender)
{
participant.ReceiveMessage(message, sender);
}
}
}
}
// The Colleague base class representing a party guest.
public abstract class PartyParticipant
{
protected IPartyMediator mediator;
public string Name { get; private set; }
public PartyParticipant(string name, IPartyMediator mediator)
{
Name = name;
this.mediator = mediator;
mediator.Register(this);
}
public abstract void ReceiveMessage(string message, PartyParticipant sender);
}
// Concrete Colleague: a guest who participates in the party.
public class Guest : PartyParticipant
{
public Guest(string name, IPartyMediator mediator) : base(name, mediator) { }
public override void ReceiveMessage(string message, PartyParticipant sender)
{
Console.WriteLine($"{Name} received a message from {sender.Name}: {message}");
}
}
// Client Code: Setting up the party.
public class Program
{
public static void Main()
{
IPartyMediator partyPlanner = new PartyPlanner();
PartyParticipant alice = new Guest("Alice", partyPlanner);
PartyParticipant bob = new Guest("Bob", partyPlanner);
PartyParticipant charlie = new Guest("Charlie", partyPlanner);
// Alice sends a message to everyone else at the party.
partyPlanner.SendMessage("Let’s hit the dance floor!", alice);
}
}
The Takeaway
The Mediator Pattern is like having an awesome party planner who manages all the interactions at your party. By centralizing communication, it decouples your objects and streamlines their interactions—ensuring your system stays organized and flexible. So next time you need to coordinate multiple components without creating a communication mess, channel your inner party planner and let the mediator do the heavy lifting!
Happy coding—and may your parties (and your code) always be a hit!