The intermediary model of design pattern

Time:2020-11-23

Mediator model

Intro

Mediator pattern, a mediation object is used to encapsulate a series of object interactions.
Mediators make the objects do not need to explicitly refer to each other, so that they are loosely coupled and can change their interactions independently.

The mediation pattern defines a single (mediation) object to encapsulate the interaction between a set of objects. Delegate the interaction between this set of objects to the interaction with the mediation object to avoid direct interaction between the objects.

Usage scenarios

The mediator pattern is generally used when a group of objects communicate in a well-defined but complex way, and where you want to customize a behavior distributed across multiple classes without generating too many subclasses.

Advantages and disadvantages

The mediator pattern is easy to be applied in the system and misused in the system. When the “many to many” interaction complex object groups appear in the system, do not rush to use the mediator mode, but first reflect on whether your system is reasonable in design.

  • advantage

    Intermediary(Mediator)The emergence of theColleagueIn order to change and reuse them independentlyColleagueClass andMediatorSecondly, because of the abstraction of how objects cooperate, the intermediary is regarded as an independent concept and encapsulated in an object, so the objects concerned are transferred from their own behavior to the interaction between them, that is, to look at the system from a more macroscopic perspective

  • shortcoming

    becauseConcreteMediatorBy controlling centralization, we change the complexity of interaction into the complexity of the mediator, which makes the mediator more complex than any otherConcreteColleagueIt’s complicated

Sample

public abstract class Mediator
{
    public abstract void Send(string message, Colleague colleague);
}

public class ConcreteMediator : Mediator
{
    public Colleague Colleague1 { private get; set; }
    public Colleague Colleague2 { private get; set; }

    public override void Send(string message, Colleague colleague)
    {
        if (colleague == Colleague1)
        {
            Colleague2.Notify(message);
        }
        else
        {
            Colleague1.Notify(message);
        }
    }
}

public abstract class Colleague
{
    protected Mediator Mediator;

    protected Colleague(Mediator mediator) => Mediator = mediator;

    public abstract void Notify(string message);
}
public class ConcreteColleague1 : Colleague
{
    public ConcreteColleague1(Mediator mediator) : base(mediator)
    {
    }

    public void Send(string message)
    {
        Mediator.Send(message, this);
    }

    public override void Notify(string message)
    {
        Console.WriteLine ($"colleague 1 gets message: {message}");
    }
}
public class ConcreteColleague2 : Colleague
{
    public ConcreteColleague2(Mediator mediator) : base(mediator)
    {
    }

    public void Send(string message)
    {
        Mediator.Send(message, this);
    }

    public override void Notify(string message)
    {
        Console.WriteLine ($"colleague 2 gets message: {message}");
    }
}


var mediator = new ConcreteMediator();

var c1 = new ConcreteColleague1(mediator);
var c2 = new ConcreteColleague2(mediator);
mediator.Colleague1 = c1;
mediator.Colleague2 = c2;

Have you had dinner? "";
C2. Send ("no, are you going to treat? "";

More

The design idea of mediation pattern is very similar to the middle layer. By introducing the intermediate layer, the interaction relationship (or dependency relationship) between a group of objects is transformed from many to many (mesh relationship) to one to many (Star relationship). In the past, an object needs to interact with n objects, but now it only needs to interact with one intermediary object, so as to minimize the interaction between objects, reduce the complexity of the code, and improve the readability and maintainability of the code.

The application of mediation model will bring some side effects, which may produce large and complex God classes.

When the interaction between the participants is complex and the maintenance cost is high, we consider using the mediation mode.

Reference

  • https://github.com/WeihanLi/DesignPatterns/tree/master/BehaviorPattern/MediatorPattern