A software in the system often contains a lot of classes, these classes will exist between the call each other, along with the expansion of system upgrade, function, these call each other relations will become very complex, and a large number of interconnected makes such a type system is unlikely to be in the absence of other class support work independently, Over time these classes become like an indivisible whole, with intricate interconnections. This makes late maintenance particularly difficult, and any major changes to the system or modules can cause unforeseen problems.

The mediator pattern

The mediator pattern solves this problem. It handles communication between different classes by providing a mediation class, which reduces the complexity of communication between multiple classes and makes code easier to maintain. The mediator pattern is a behavioral pattern. Through the application of Mediator mode, the many-to-many relationship between classes can be transformed into one-to-many relationship, thus reducing the coupling between classes.

GOF describes the intermediary model as: Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.. — Design Patterns: Elements of Reusable Object-oriented Software

UML class diagram:

Code implementation

public interface IMediator<T> { void Operation(); void Register(IColleague<T> provider, params IColleague<T>[] consumers); } public interface IColleague<T> { T Data { get; set; } IMediator<T> Mediator { get; set; } } public abstract class ColleagueBase<T> : IColleague<T> { public virtual T Data { get; set; } private IMediator<T> mediator; public virtual IMediator<T> Mediator { get { return mediator; } set { mediator = value; } } } public class Mediator<T> : IMediator<T> { private IColleague<T> provider; private IList<IColleague<T>> consumers; public void Operation() { if (provider ! = null && consumers ! = null && consumers.Count > 0) { foreach (var item in consumers) { item.Data = provider.Data; } } } public void Register(IColleague<T> provider, params IColleague<T>[] consumers) { this.provider = provider; if (consumers ! = null && consumers.Length > 0) { this.consumers = new List<IColleague<T>>(consumers); } } } public class ConcreteColleagueA : ColleagueBase<int> { public override int Data { get => base.Data; set { base.Data = value; base.Mediator.Operation(); } } } public class ConcreteColleagueB : ColleagueBase<int> { } public class ConcreteColleagueC : ColleagueBase<int> { }Copy the code

Calls to end:

public class Test
{
    public static void Entry()
    {
        Mediator<int> mA2BC = new Mediator<int>();
        ConcreteColleagueA a = new ConcreteColleagueA();
        ConcreteColleagueB b = new ConcreteColleagueB();
        ConcreteColleagueC c = new ConcreteColleagueC();

        a.Mediator = b.Mediator = c.Mediator = mA2BC;
        mA2BC.Register(a, b, c);
        a.Data = 20;
        Console.WriteLine($"a:{a.Data},b:{b.Data}, c:{c.Data}"); //a:20,b:20,c:20

        mA2BC.Register(a, b);
        a.Data = 30;
        Console.WriteLine($"a:{a.Data},b:{b.Data}, c:{c.Data}"); //a:30,b:30,c:20
    }
}
Copy the code

Applicable scenario

  • There are complex reference relationships among objects in the system, which leads to the disorganized structure of the dependency relationship between them and the difficulty of reuse the object.
  • You want an intermediate class to encapsulate behavior in multiple classes without generating too many subclasses.

The advantages and disadvantages

advantages

  • The complexity of system classes is reduced, and one-to-many becomes one-to-one, requiring only interaction with the mediator
  • The mediator pattern decouples systems or modules from each other by eliminating the need for explicit references between objects.
  • It fits the Demeter principle

Disadvantages Because the intermediary object encapsulates the interaction between various classes, the complexity of the intermediary class itself will increase, and there will be some maintenance costs

Reference book: design patterns — implementation and extension of engineering based on C# by wang xiang