Design Patterns (.NET)- Mediator Pattern Part-4

Mediator Design Pattern is a behavioral design pattern that is used to reduce the communication between objects and it forces them to communicate via a mediator.

Mediator pattern promotes loose coupling by keeping objects from referring to each other explicitly and it lets you vary the interaction independently. It reduces the complexity of communication between objects.


Concept : Here, a mediator object normally handles the complexity of communication between different objects by acting as a communication center of all other objects. It means whenever an object needs to communicate with another object, it doesn’t directly call the other objects. Instead of that, it calls the mediator object and it is the responsibility of the mediator object to route the message to the destination object.

No alt text provided for this image

Mediator Pattern Implementational Diagram:?

No alt text provided for this image

Mediator: It is an interface and it defines all possible interactions between colleagues.

ConcreteMediator: It is a class that implements the Mediator interface and coordinates communication between colleague objects.

Colleague: It is an abstract class and this abstract class is going to be implemented by Concrete Colleague classes.

ConcreteColleage1 / ConcreteColleage2: These are classes and implement the Colleague interface. If a concrete colleague (let say ConcreteColleage1) wants to communicate with another concrete colleague (let say ConcreteColleage2), they will not communicate directly instead they will communicate via the ConcreteMediator.

Problem

Services or classes often have several dependencies on other classes and you quickly end up with a big chaos of dependencies. The mediator pattern serves as an organizer and calls all needed services. No service has a dependency on another one, only on the mediator.

You can see the mediator pattern also in real life. Think about a big airport like JFK with many arriving and departing planes. They all need to be coordinated to avoid crashes. It would be impossible for a plan to talk to all other planes. Instead, they call the tower, their mediator, and the tower talks to all planes and organizes who goes where.

Advantages and Disadvantages of the Mediator Pattern

The mediator pattern brings a couple of advantages:

  • Less coupling: Since the classes don’t have dependencies on each other, they are less coupled.
  • Easier reuse: Fewer dependencies also help to reuse classes.
  • Single Responsibility Principle: The services don’t have any logic to call other services, therefore they only do one thing.
  • Open/closed principle: Adding new mediators can be done without changing the existing code.

There is also one big disadvantage of the mediator pattern:

  • The mediator can become such a crucial factor in your application that it is called a “god class”.

Implementional Example :

Creating Mediato

namespace MediatorDesignPattern

{public interface FacebookGroupMediator

???{void SendMessage(string msg, User user);

????void RegisterUser(User user);}

}

Creating ConcreteMediator

using System.Collections.Generic;

namespace MediatorDesignPattern

{

???public class ConcreteFacebookGroupMediator : FacebookGroupMediator

???{

???????private List<User> usersList = new List<User>();

???????public void RegisterUser(User user)

???????{

???????????usersList.Add(user);

???????}

???????public void SendMessage(string message, User user)

???????{

???????????foreach (var u in usersList)

???????????{

???????//message should not be received by the user sending it

???????????????if (u != user)

???????????????{

???????????????????u.Receive(message);

???????????????}

???????????}

???????}

???}

}







Creating Colleague




namespace MediatorDesignPattern

{

???public abstract class User

???{

???????protected FacebookGroupMediator mediator;

???????protected string name;

???????public User(FacebookGroupMediator mediator, string name)

???????{

???????????this.mediator = mediator;

???????????this.name = name;

???????}

???????public abstract void Send(string message);

???????public abstract void Receive(string message);

???}

}




Creating ConcreteColleague




using System;

namespace MediatorDesignPattern

{

???public class ConcreteUser : User

???{

???????public ConcreteUser(FacebookGroupMediator mediator, string name) : base(mediator, name)

???????{

???????}

???????public override void Receive(string message)

???????{

???????????Console.WriteLine(this.name + ": Received Message:" + message);

???????}

???????public override void Send(string message)

???????{

???????????Console.WriteLine(this.name + ": Sending Message=" + message + "\n");

???????????mediator.SendMessage(message, this);

???????}

???}




Main Class to create the other object via mediator and get the Required output

using System;

namespace MediatorDesignPattern

{

???class Program

???{

???????static void Main(string[] args)

???????{

???????????FacebookGroupMediator facebookMediator = new ConcreteFacebookGroupMediator();

???????????User Ram = new ConcreteUser(facebookMediator, "Ram");

???????????User Dave = new ConcreteUser(facebookMediator, "Dave");

???????????User Smith = new ConcreteUser(facebookMediator, "Smith");

???????????User Rajesh = new ConcreteUser(facebookMediator, "Rajesh");

???????????User Sam = new ConcreteUser(facebookMediator, "Sam");

???????????User Pam = new ConcreteUser(facebookMediator, "Pam");

???????????User Anurag = new ConcreteUser(facebookMediator, "Anurag");

???????????User John = new ConcreteUser(facebookMediator, "John");

???????????facebookMediator.RegisterUser(Ram);

???????????facebookMediator.RegisterUser(Dave);

???????????facebookMediator.RegisterUser(Smith);

???????????facebookMediator.RegisterUser(Rajesh);

???????????facebookMediator.RegisterUser(Sam);

???????????facebookMediator.RegisterUser(Pam);

???????????facebookMediator.RegisterUser(Anurag);

???????????facebookMediator.RegisterUser(John);

???????????Dave.Send("dotnettutorials.net - this website is very good to learn Design Pattern");

???????????Console.WriteLine();

???????????Rajesh.Send("What is Design Patterns? Please explain ");

???????????Console.Read();

???????}

???}

}


        

Expected output

No alt text provided for this image

I hope,? through this article you have got a little idea about Mediator Design pattern and the Use of Mediator Design? Pattern

要查看或添加评论,请登录

Foysal Ahamed Sifat的更多文章

社区洞察

其他会员也浏览了