05 Dec 2023



Intermediate

The Mediator design pattern is a behavioral pattern that defines an object that centralizes communication between objects in a system. Instead of objects communicating directly with each other, they communicate through a mediator. In C#, the Mediator pattern typically involves the following main components:

  1. Mediator Interface:

    • Declares the communication interface that concrete mediators must implement.
    • Typically, this interface includes methods for registering, notifying, and handling components.
    public interface IMediator
    {
        void RegisterComponent(Component component);
        void Notify(Component sender, string message);
    }
    
  2. Concrete Mediator:

    • Implements the IMediator interface.
    • Manages the communication between components.
    • Keeps references to all components in the system.
    public class ConcreteMediator : IMediator
    {
        private List<Component> components = new List<Component>();
    
        public void RegisterComponent(Component component)
        {
            components.Add(component);
        }
    
        public void Notify(Component sender, string message)
        {
            foreach (var component in components)
            {
                if (component != sender)
                {
                    component.Receive(message);
                }
            }
        }
    }
    
  3. Colleague Interface:

    • Declares the interface for communication with the mediator.
    • Typically includes a method for receiving messages from the mediator.
    public interface IColleague
    {
        void SetMediator(IMediator mediator);
        void Send(string message);
        void Receive(string message);
    }
    
  4. Concrete Colleague:

    • Implements the IColleague interface.
    • Communicates with the mediator when it wants to send or receive messages.
    public class Component : IColleague
    {
        private IMediator mediator;
    
        public void SetMediator(IMediator mediator)
        {
            this.mediator = mediator;
        }
    
        public void Send(string message)
        {
            Console.WriteLine($"Component sends: {message}");
            mediator.Notify(this, message);
        }
    
        public void Receive(string message)
        {
            Console.WriteLine($"Component receives: {message}");
        }
    }
    
  5. Client:

    • Creates instances of concrete colleagues and a concrete mediator.
    • Sets the mediator for each colleague.
    • Initiates communication by calling methods on the colleagues.
    class Client
    {
        static void Main()
        {
            ConcreteMediator mediator = new ConcreteMediator();
    
            Component component1 = new Component();
            Component component2 = new Component();
    
            mediator.RegisterComponent(component1);
            mediator.RegisterComponent(component2);
    
            component1.SetMediator(mediator);
            component2.SetMediator(mediator);
    
            component1.Send("Hello from Component1");
            component2.Send("Hi from Component2");
        }
    }
    

In this example:

  • IMediator is the mediator interface, declaring methods for registering components and notifying them.
  • ConcreteMediator is a concrete implementation of the mediator, managing communication between components.
  • IColleague is the colleague interface, declaring methods for setting the mediator, sending messages, and receiving messages.
  • Component is a concrete implementation of the colleague, communicating with the mediator to send and receive messages.
  • The Client creates instances of the concrete colleagues and mediator, sets the mediator for each colleague, and initiates communication by calling methods on the colleagues.

The Mediator pattern helps to reduce the direct dependencies between objects by centralizing communication through a mediator. This promotes a more loosely coupled system and simplifies the maintenance and extension of the code.

software-design-patterns
mediator-design-pattern
c#