Skip to content

Latest commit

 

History

History
118 lines (92 loc) · 5.49 KB

mediator.md

File metadata and controls

118 lines (92 loc) · 5.49 KB

Mediator

The Mediator design pattern is a behavioral design pattern that promotes loose coupling among a group of objects by introducing a mediator object that encapsulates the communication between them. It allows objects to interact with each other indirectly through the mediator, rather than directly referencing and communicating with each other.

The main purpose of the Mediator pattern is to reduce the dependencies between the participating objects, making it easier to maintain and modify their interactions. Instead of each object knowing about and communicating with multiple other objects, they only need to be aware of the mediator object. The mediator acts as a central hub that coordinates the communication and interaction between the objects.

Here's a general overview of how the Mediator pattern works:

  1. Participants: The system consists of multiple objects, often referred to as "colleagues" or "participants," that need to communicate with each other.

  2. Mediator: The mediator is an interface or an abstract class that defines the communication protocol between the objects. It declares methods that facilitate the interaction between the objects.

  3. Concrete Mediator: The concrete mediator implements the mediator interface and manages the communication and coordination between the participating objects. It keeps references to all the objects and handles the logic of how they interact with each other.

  4. Colleagues: Each object in the system is a colleague and is aware of the mediator object. When a colleague wants to communicate with another colleague, it doesn't directly reference the other colleague but instead sends the message or request to the mediator.

  5. Communication: When a colleague sends a message or request to the mediator, the mediator receives it and decides how to handle it. It may forward the message to other colleagues or perform additional processing based on the system's requirements.

By using the Mediator pattern, the objects in a system become decoupled, as they only need to know about and interact with the mediator. This allows for better maintainability, extensibility, and reusability of the objects, as changes to the communication logic can be confined to the mediator without affecting the individual objects.

It's worth noting that the Mediator pattern is particularly useful in complex systems or scenarios where a large number of objects need to communicate with each other in a structured and organized manner.

Let's consider an example of a chat application where multiple users can send messages to each other. In this scenario, the Mediator pattern can be used to facilitate communication between the users.

using System;
using System.Collections.Generic;

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

// Concrete Mediator
public class ChatMediator : IChatMediator
{
    private List<User> users = new List<User>();

    public void AddUser(User user)
    {
        users.Add(user);
    }

    public void SendMessage(string message, User sender)
    {
        foreach (var user in users)
        {
            if (user != sender)
            {
                user.ReceiveMessage(message);
            }
        }
    }
}

// Colleague
public class User
{
    private string name;
    private IChatMediator chatMediator;

    public User(string name, IChatMediator chatMediator)
    {
        this.name = name;
        this.chatMediator = chatMediator;
    }

    public void SendMessage(string message)
    {
        Console.WriteLine($"{name} sent a message: {message}");
        chatMediator.SendMessage(message, this);
    }

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

// Usage
public class Program
{
    public static void Main(string[] args)
    {
        // Create mediator
        IChatMediator chatMediator = new ChatMediator();

        // Create users and add them to the mediator
        User user1 = new User("John", chatMediator);
        User user2 = new User("Alice", chatMediator);
        User user3 = new User("Bob", chatMediator);
        chatMediator.AddUser(user1);
        chatMediator.AddUser(user2);
        chatMediator.AddUser(user3);

        // Users send messages through the mediator
        user1.SendMessage("Hello, everyone!");
        user2.SendMessage("Hey, John!");

        /*
        Output:
        John sent a message: Hello, everyone!
        Alice received a message: Hello, everyone!
        Bob received a message: Hello, everyone!
        John sent a message: Hey, John!
        Alice received a message: Hey, John!
        Bob received a message: Hey, John!
        */
    }
}

In this example, the IChatMediator interface represents the mediator, which declares methods for sending messages and adding users. The ChatMediator class is the concrete mediator that maintains a list of users and handles the communication between them.

The User class represents a colleague, which has a reference to the mediator and can send and receive messages. When a user sends a message, it calls the SendMessage method of the mediator, passing the message and the sender as arguments. The mediator then relays the message to all the other users except the sender.

By using the Mediator pattern, the users don't need to directly communicate with each other. They rely on the mediator to handle the message distribution, which promotes loose coupling and simplifies the communication process.