Mediator - Behavioural


Overview:

The Mediator Pattern in C# transforms your code into a love story orchestrator—connecting components with playful elegance. Let's explore this charming coding narrative:

Implementation in C#:

In C#, the Mediator Pattern involves defining a mediator interface to encapsulate the communication between components. Consider a chatroom scenario where participants communicate through a mediator:

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

// Concrete mediator
public class ChatMediator : IChatMediator
{
    public void SendMessage(string message, Participant sender)
    {
        // Broadcast message to other participants
        foreach (var participant in participants)
        {
            if (participant != sender)
            {
                participant.ReceiveMessage(message);
            }
        }
    }

    // Other mediator logic and participant management
}

// Participant
public class Participant
{
    private readonly IChatMediator mediator;
    public string Name { get; }

    public Participant(IChatMediator mediator, string name)
    {
        this.mediator = mediator;
        Name = name;
    }

    public void SendMessage(string message)
    {
        mediator.SendMessage($"{Name}: {message}", this);
    }

    public void ReceiveMessage(string message)
    {
        Console.WriteLine($"Received message: {message}");
    }
}

Pros:

  1. Decouples Components: Removes direct dependencies between components, promoting loose coupling.

  2. Centralized Communication: Centralizes communication logic, making it easier to manage.

  3. Promotes Reusability: Encourages the reuse of individual components in different scenarios.

Cons:

  1. Complexity: Introducing a mediator may increase initial code complexity.

  2. Mediator Overhead: A heavily loaded mediator can become a bottleneck.

When to Use and When Not:

  • Use: When you want to decouple components, centralize communication, or facilitate reuse of components.

  • Avoid: In simpler scenarios where direct communication between components is sufficient or when a centralized mediator introduces unnecessary complexity.

Usage in .NET Core Framework:

While the Mediator Pattern is not explicitly present in the .NET Core framework, its principles are reflected in various design patterns and architectural choices. Event-driven programming in frameworks like ASP.NET Core and the use of event aggregators showcase mediation-like behavior.

Real-Life Example:

Consider a user interface where different UI components need to communicate without direct dependencies. The Mediator Pattern can be applied:

// Mediator for UI components
var uiMediator = new UIMediator();

// UI components
var button = new Button(uiMediator);
var textbox = new TextBox(uiMediator);

// User clicks the button
button.Click();

// Mediator broadcasts the event to other components

Here, the UIMediator facilitates communication between the button and textbox without them being directly aware of each other.

In conclusion, the Mediator Pattern in C# transforms your code into Code Cupid's playful orchestrator—connecting components with elegance. While decoupling and centralizing communication, developers should balance its introduction with potential complexity. Although not explicitly found in the .NET Core framework, its influence resonates in event-driven programming and architectural choices, making it a charming choice for scenarios where components need to dance to a coordinated love story without stepping on each other's toes.


No files yet, migration hasn't completed yet!