Categories: Design Pattern

Behavioral Patterns – Mediator

Mediator – It can let the program reduces chaotic dependencies between objects. This pattern can restrict the direct communications between the object and forces them to collaborates with a mediator object.

From Design Pattern – Behavioral Patterns

Structure

Mediator pattern – Class diagram UML

Applicability

  • Use its pattern when the program is hard to change because it is tightly a couple of a bunch of other classes.
  • Use its pattern when the programs can’t reuse a component in a different schedule because it’s too dependent on other components.
  • Use its pattern when the programs create tons of component subclass to reuse the same behavior in various contexts.

Pros and Cons

PROS

  • Single responsibility principle: The program can extract the communication between various components into a single place, making it easier to comprehend and maintain.
  • Open-closed principle: It can introduce new mediators without having to change the essential components.
  • It can reduce coupling between various components in the program.
  • It can reuse individual components more quickly.

CONS

  • Over time a mediator can evolve into a god object.

How to Implement

#1 Basic Sample

static void Main(string[] args)
{
 Component1 obj1 = new Component1();
 Component2 obj2 = new Component2();

 new ConcerteMediator(obj1, obj2);

 Console.WriteLine("Client triggets operation A.");
 obj1.DoA();

 Console.WriteLine();

 Console.WriteLine("Client triggers operation C.");
 obj2.DoC();

 Console.ReadKey();
}

interface IMediator
{
 void Notify(object sender, string ev);
}

class ConcerteMediator : IMediator
{
 private Component1 component1;
     private Component2 component2;

     public ConcerteMediator(Component1 obj1, Component2 obj2)
     {
  component1 = obj1;
  component1.SetMediator(this);
  component2 = obj2;
  component2.SetMediator(this);
     }

     public void Notify(object sender, string ev)
     {
  switch (ev)
  {
       case "A":
   Console.WriteLine("Mediator reacts on A and triggers folowing operations:");
   component2.DoC();
   break;

       case "C":
   Console.WriteLine("Mediator reacts on C and triggers following operations:");
   break;
  }
     }
}

class BaseComponent
{
     protected IMediator mediator;

     public BaseComponent(IMediator _mediator = null) => mediator = _mediator;

     public void SetMediator(IMediator _mediator) => mediator = _mediator;
    
}

class Component1 : BaseComponent
{
     public void DoA()
     {
  Console.WriteLine("Component 1 does A.");

  mediator.Notify(this, "A");
     }

     public new void SetMediator(IMediator _mediator) => mediator = _mediator;
}

class Component2 : BaseComponent
{
     public void DoC()
     {
  Console.WriteLine("Component 2 does C.");

  mediator.Notify(this, "C");
     }

     public new void SetMediator(IMediator _mediator) => mediator = _mediator;
}

#2 Web Application Project

Step 1 Create the mediator pattern’s interface class files in the Core projects. One interface class file name sets “IMediator.cs.” Another interface class file name sets “IMediatorExe.cs.”

Fig 1 Interface class solution explorer
Fig 2 IMediatorExe interface class file source code
Fig 2 IMediator interface class file source code

Step 2 Create the mediator pattern-related files in the Core projects, such as the “BaseComponent.cs”, the “Mediator.cs”, the “Component1.cs” and the “Component2.cs.”

Fig 3 Mediator pattern-related files – Solution explorer

The “BaseComponent.cs” file doesn’t need to inherit any class files. The “IMediator” variables direct statement, which is a “protected” variable, then adding the constructor mechanism and “SetMediator” function in its class.

Fig 4 BaseComponent class file source code

The component class flies inherits the “BaseComponent.cs” file. Every class files add the constructor mechanism, then adds a few operation functions.

Fig 5 Component1 class file source code
Fig 6 Component2 class file source code

The “Mediator.cs” file inherits the “IMediator” class files, then adds the constructor mechanism.

Fig 7 Mediator class file source code.

Step 3 Create the “IMediatorExe” class’s service class flies in the Core projects. This services class file name sets “MediatorServices.cs.” Its file inherits the “ImediatorExe” interface class flies. Suppose the data object is not null before executing the mediator pattern operation function.

Fig 8 MediatorServices class file source code

Step 4 Add the dispose mechanism by the IDispose class methods in every one class file in the Core project.

Step 5 Add the iterator pattern-related files in the web project, such as view flies, controller files. Register the iterator pattern files in the web project’s start.cs file.

Fig 9 Mediator controller file source code
Fig 10 Mediator pattern result

Reference

davidsky69

View Comments

Recent Posts

API Gateway in .NET 5 with Ocelot

What is the API gateway? An API gateway is an API management tool that sits…

3 years ago

.NET 5 application with Onion architecture

The .NET 5 SDK is a kind of milestone in the .NET world. The .NET…

4 years ago

SOLID Principles – Dependency inversion principle

In object-oriented design, the dependency inversion principle is a specific methodology for loosely coupling software…

4 years ago

SOLID Principles – Interface segregation principle

In the field of software engineering, the interface segregation principle (ISP) states that no code…

4 years ago

SOLID Principles – Liskov substitution principle

Subtype Requirement: Let  be a property provable about objects  of type T. Then  should be true for objects  of type S where S is…

4 years ago

SOLID Principles – Open-closed principle

In object-oriented programming, the open–closed principle states "software entities (classes, modules, functions, etc.) should be…

4 years ago