In my last article about the proxy pattern we have completed the structural category. It's time to move toward the final category, which is the behavioral category.

Agenda

Previous Articles

  1. Design Patterns - Introduction
  2. Learn Design Pattern - Singleton Pattern
  3. Learn Design Pattern - Factory Method Pattern
  4. Learn Design Pattern - Abstract Factory Pattern
  5. Learn Design Pattern - Builder Pattern
  6. Learn Design Pattern - Prototype Pattern
  7. Learn Design Pattern - Adapter Pattern
  8. Learn Design Pattern - Composite Pattern
  9. Learn Design Pattern - Decorator Pattern
  10. Learn Design Pattern – Facade Pattern
  11. Learn Design Pattern - Flyweight Pattern
  12. Learn Design Pattern - Bridge Pattern
  13. Learn Design Pattern – Proxy Pattern

(I have also conducted online training on design patterns and other. NET based technologies. You can contact me at 9870148461 or at [email protected])

What is mediator pattern?

When to use?

Consider a situation where we have many classes encapsulating specific logic inside them (like invoicing, inventory management, payroll).

Usually these classes interact with each other. Over a period of time communication among these classes can become increasingly complex and the communication logic becomes scattered throughout multiple classes which creates management hell.

mediator-design-pattern.gif

C# Corner Mumbai group

In the first scenario there is Shivprasad sir who is acting as a Mediator and controlling the communication between other members. Whereas in the second scenario there is no Mediator so the interaction was difficult and so everything gets messed.

In short if there is no mediator then every object must know about every other object. (They are tightly coupled.)

For online training contact [email protected]

Chat application using Mediator Pattern

Let's build a small chat-based application.

Note:

Let's start coding

Our application may have two kinds of chat users. One who used to communicate via Mobile and another via Desktop/Laptop.

application-chat-users.gif

Final Output

Chat-application-Mediator-Pattern.gif

Step 1

Create an abstract user, as:

public abstract class ClsUser
{
internal string userName
{
get;
set;
}
internal ClsAbstractMediator objMediator;
public ClsUser(string userName)
{
this.userName = userName;
}
public abstract void Send(string toUser, string message);
public abstract void Receive(string fromUser, string message);
}


Step 2

Create an abstract mediator, as:

public abstract class ClsAbstractMediator
{
public abstract void AddUser(ClsUser user);
public abstract void RemoveUser(string userName);
public abstract void SendMessage(string fromUser, string toUser, string message);
}

Step 3

Create concreate users, as:

public class ClsDesktopUser : ClsUser
{
public override void Receive(string fromUser, string message)
{
Console.WriteLine(this.userName + ":" + message);
}
public override void Send(string toUser, string message)
{
objMediator.SendMessage(toUser, this.userName, message);
}
}
public class ClsMobileUser : ClsUser
{
public override void Receive(string fromUser, string message)
{
Console.WriteLine(this.userName + ":" + message);
}
public override void Send(string toUser, string message)
{
objMediator.SendMessage(this.userName, toUser, message);
}
}

Step 4

The final Client Code:

ClsAbstractMediator objMediator = new ClsMediator();
ClsUser userA = new ClsMobileUser("userA");
ClsUser userB = new ClsMobileUser("userB");
ClsUser userC = new ClsDesktopUser("userC");
ClsUser userD= new ClsDesktopUser("userD");
objMediator.AddUser(userA);
objMediator.AddUser(userB);
objMediator.AddUser(userC);
objMediator.AddUser(userD);
userA.Send("userB", "Hi userB, can you help me to understand mediator pattern");
userB.Send("userA", "sorry man, even i m trying to understand the same.Try to consult userC");
userA.Send("userB", "OK\n");
userA.Send("userC", "hey userC can you help me.");
userC.Send("userA","sorry bro, m busy right now, working on an article,will ping in some time ;)");
userC.Send("userA","As usual :P");

Now users are interacting with each other with the help of a mediator.

(You can see that if we want, we can even change the mediator easily as we are working with interfaces (ClsAbstractMediator) instead of concrete classes (ClsMediator)).

Class Diagram

mediator-pattern-class-diagrame.gif

Hope all of you enjoyed reading this article.

Feedbacks and suggestions are always welcome.

Subscribe for my all article updates at

https://www.facebook.com/pages/Blogs-By-Sukesh-Marla/168078149903213

Keep learning and keep coding.