mediator pattern in csharp

The Mediator pattern is used to reduce interdependencies between objects by introducing a mediator object that handles the communication between them. This allows for a more modular and decoupled design. Here's an example implementation in C#:

main.cs
// Define mediator interface
public interface IMediator
{
    void Notify(object sender, string ev);
}

// Define concrete mediator
public class ConcreteMediator : IMediator
{
    private Component1 component1;
    private Component2 component2;

    public ConcreteMediator(Component1 component1, Component2 component2)
    {
        this.component1 = component1;
        this.component1.SetMediator(this);
        this.component2 = component2;
        this.component2.SetMediator(this);
    }

    public void Notify(object sender, string ev)
    {
        if (ev == "A")
        {
            Console.WriteLine("Mediator reacts on A and triggers following operations:");
            this.component2.DoC();
        }
        if (ev == "D")
        {
            Console.WriteLine("Mediator reacts on D and triggers following operations:");
            this.component1.DoB();
            this.component2.DoC();
        }
    }
}

// Define components
public class BaseComponent
{
    protected IMediator mediator;

    public BaseComponent(IMediator mediator = null)
    {
        this.mediator = mediator;
    }

    public void SetMediator(IMediator mediator)
    {
        this.mediator = mediator;
    }
}

public class Component1 : BaseComponent
{
    public void DoA()
    {
        Console.WriteLine("Component 1 does A.");
        this.mediator.Notify(this, "A");
    }

    public void DoB()
    {
        Console.WriteLine("Component 1 does B.\n");
        this.mediator.Notify(this, "B");
    }
}

public class Component2 : BaseComponent
{
    public void DoC()
    {
        Console.WriteLine("Component 2 does C.");
        this.mediator.Notify(this, "C");
    }

    public void DoD()
    {
        Console.WriteLine("Component 2 does D.");
        this.mediator.Notify(this, "D");
    }
}

// Example usage
public class Client
{
    static void Main(string[] args)
    {
        Component1 component1 = new Component1();
        Component2 component2 = new Component2();

        ConcreteMediator mediator = new ConcreteMediator(component1, component2);

        Console.WriteLine("Client triggers operation A.");
        component1.DoA();

        Console.WriteLine("\nClient triggers operation D.");
        component2.DoD();
    }
}
2252 chars
100 lines

In this example, the ConcreteMediator class is responsible for handling communication between the Component1 and Component2 classes. The Notify() method is called by the components to send messages to the mediator, which then sends messages to the other component as needed.

The Client class demonstrates how the mediator pattern can be used in practice, by creating instances of the components and mediator, and then triggering their methods to communicate with each other.

gistlibby LogSnag