Lädt...


🔧 C# Design Pattern: Mediator


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

The Mediator pattern is used to reduce direct dependencies between objects by introducing a mediator object that controls communication between them. This is useful when you have many objects that need to communicate, but you don’t want them to be tightly coupled to each other. The Mediator centralizes this communication, making the code more flexible and easier to maintain.

C# Code Example:

// Mediator interface
public interface IMediator
{
    void SendMessage(string message, Colleague colleague);
}

// Base class for colleagues that communicate through the mediator
public abstract class Colleague
{
    protected IMediator _mediator;

    public Colleague(IMediator mediator)
    {
        _mediator = mediator;
    }

    public abstract void ReceiveMessage(string message);
}

// Concrete Colleague 1
public class ConcreteColleague1 : Colleague
{
    public ConcreteColleague1(IMediator mediator) : base(mediator) { }

    public void Send(string message)
    {
        Console.WriteLine($"Colleague 1 sending message: {message}");
        _mediator.SendMessage(message, this);
    }

    public override void ReceiveMessage(string message)
    {
        Console.WriteLine($"Colleague 1 received message: {message}");
    }
}

// Concrete Colleague 2
public class ConcreteColleague2 : Colleague
{
    public ConcreteColleague2(IMediator mediator) : base(mediator) { }

    public void Send(string message)
    {
        Console.WriteLine($"Colleague 2 sending message: {message}");
        _mediator.SendMessage(message, this);
    }

    public override void ReceiveMessage(string message)
    {
        Console.WriteLine($"Colleague 2 received message: {message}");
    }
}

// Concrete Mediator managing the communication
public class ConcreteMediator : IMediator
{
    private ConcreteColleague1 _colleague1;
    private ConcreteColleague2 _colleague2;

    public ConcreteColleague1 Colleague1
    {
        set { _colleague1 = value; }
    }

    public ConcreteColleague2 Colleague2
    {
        set { _colleague2 = value; }
    }

    public void SendMessage(string message, Colleague colleague)
    {
        if (colleague == _colleague1)
        {
            _colleague2.ReceiveMessage(message);
        }
        else
        {
            _colleague1.ReceiveMessage(message);
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Create the mediator
        ConcreteMediator mediator = new ConcreteMediator();

        // Create colleagues
        ConcreteColleague1 colleague1 = new ConcreteColleague1(mediator);
        ConcreteColleague2 colleague2 = new ConcreteColleague2(mediator);

        // Set colleagues in the mediator
        mediator.Colleague1 = colleague1;
        mediator.Colleague2 = colleague2;

        // Colleague 1 sends a message
        colleague1.Send("Hello from Colleague 1");

        // Colleague 2 sends a message
        colleague2.Send("Hi from Colleague 2");
    }
}

Code Explanation:

In this example, the ConcreteMediator manages communication between two colleagues, ConcreteColleague1 and ConcreteColleague2. When one colleague sends a message, the mediator decides which colleague to send the message to. This reduces direct coupling between the colleagues since they don’t need to know about each other, only the mediator.

Conclusion:

The Mediator pattern is useful when you want to centralize communication between multiple objects, preventing them from being directly coupled. This makes the code more modular and easier to modify, as you can change the communication logic without altering the individual objects.

Source code: GitHub

...

🔧 C# Design Pattern: Mediator


📈 39.86 Punkte
🔧 Programmierung

🔧 C# Design Pattern: Mediator (Portugues)


📈 39.86 Punkte
🔧 Programmierung

🔧 CQRS and Mediator pattern


📈 34.61 Punkte
🔧 Programmierung

🔧 Understanding the Mediator Pattern and CQRS with dart_mediatR


📈 34.61 Punkte
🔧 Programmierung

🔧 Understanding the Mediator Pattern in .NET


📈 34.61 Punkte
🔧 Programmierung

🔧 JavaScript Design Patterns - Behavioral - Mediator


📈 28.96 Punkte
🔧 Programmierung

🔧 Implementing Chain of Responsibility Pattern in C# : Middleware's Design Pattern


📈 27.06 Punkte
🔧 Programmierung

🔧 Design Pattern #7 - Builder Pattern


📈 27.06 Punkte
🔧 Programmierung

🔧 Design Pattern #6 - Composite Pattern


📈 27.06 Punkte
🔧 Programmierung

🔧 Design Pattern #5 - Adapter Pattern


📈 27.06 Punkte
🔧 Programmierung

🔧 Module-Pattern | Javascript Design Pattern Simplified | Part 4


📈 27.06 Punkte
🔧 Programmierung

🔧 Decorator-Pattern | Javascript Design Pattern Simplified | Part 5


📈 27.06 Punkte
🔧 Programmierung

🔧 Factory-Pattern | Javascript Design Pattern Simplified | Part 2


📈 27.06 Punkte
🔧 Programmierung

🔧 Observer-Pattern | Javascript Design Pattern Simplified | Part 3


📈 27.06 Punkte
🔧 Programmierung

🔧 Singleton-Pattern | Javascript Design Pattern Simplified | Part 1


📈 27.06 Punkte
🔧 Programmierung

🔧 Design Pattern #4 - Publisher/Subscriber Pattern


📈 27.06 Punkte
🔧 Programmierung

🔧 Design Pattern #3 - Observer Pattern


📈 27.06 Punkte
🔧 Programmierung

🔧 Design Pattern #2 - Facade Pattern


📈 27.06 Punkte
🔧 Programmierung

🐧 Observer Pattern: Was steckt hinter dem Observer Design Pattern?


📈 27.06 Punkte
🐧 Server

🔧 Mediator and CQRS with MediatR


📈 23.71 Punkte
🔧 Programmierung

🔧 KI als Mediator? Diese Studie zeigt, wie gut künstliche Intelligenz Streitigkeiten schlichten kann


📈 23.71 Punkte
🔧 Programmierung

📰 KI als Mediator? Diese Studie zeigt, wie gut künstliche Intelligenz Streitigkeiten schlichten kann


📈 23.71 Punkte
📰 IT Nachrichten

📰 KI als Mediator? Diese Studie zeigt, wie gut künstliche Intelligenz Streitigkeiten schlichten kann


📈 23.71 Punkte
📰 IT Nachrichten

🔧 Mediator


📈 23.71 Punkte
🔧 Programmierung

🕵️ CVE-2010-0596 | Cisco Mediator Framework up to 2.1 Privilege Escalation (VU#757804 / SBV-25915)


📈 23.71 Punkte
🕵️ Sicherheitslücken

🕵️ CVE-2010-0597 | Cisco Mediator Framework up to 2.1 denial of service (VU#757804 / SBV-25916)


📈 23.71 Punkte
🕵️ Sicherheitslücken

🕵️ INTER-Mediator 5.5 resetpassword.php c/cred cross site scripting


📈 23.71 Punkte
🕵️ Sicherheitslücken

🕵️ INTER-Mediator 5.5 resetpassword.php c/cred Cross Site Scripting


📈 23.71 Punkte
🕵️ Sicherheitslücken

⚠️ [dos] Pdfium - Out-of-Bounds Read with Shading Pattern Backed by Pattern Colorspace


📈 21.81 Punkte
⚠️ PoC

matomo