What is mediator pattern?
According to the GOF the "Mediator Pattern defines 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 let's you vary their interaction independently.
When to use?
Consider a situation where we have many classes encapsulating specific logic inside them (like invoicing, inventory management, payroll).
*
* Chat application using Mediator Pattern
Let's build a small chat-based application.
Note:
This will not be a real-time chat application.
***** A real chat application requires sockets or other network-based programming along with a Mediator pattern.
* ***A Mediator Pattern or any other design pattern we have already discussed are not rules, and its not even mandatory that it be used in every application.
* Try to understand your problem first and then see which design pattern satisfies your requirements.
class Program
{
static void Main(string[] args)
{
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");
}
}
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);
}
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);
}
public class ClsDesktopUser : ClsUser
{
public override void Send(string toUser, string message)
{
objMediator.SendMessage(toUser, this.userName, message);
}
public override void Receive(string fromUser, string message)
{
Console.WriteLine(this.userName + ":" + message);
}
}
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);
}
}
According to the GOF the "Mediator Pattern defines 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 let's you vary their interaction independently.
When to use?
Consider a situation where we have many classes encapsulating specific logic inside them (like invoicing, inventory management, payroll).
*
* Chat application using Mediator Pattern
Let's build a small chat-based application.
Note:
This will not be a real-time chat application.
***** A real chat application requires sockets or other network-based programming along with a Mediator pattern.
* ***A Mediator Pattern or any other design pattern we have already discussed are not rules, and its not even mandatory that it be used in every application.
* Try to understand your problem first and then see which design pattern satisfies your requirements.
class Program
{
static void Main(string[] args)
{
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");
}
}
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);
}
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);
}
public class ClsDesktopUser : ClsUser
{
public override void Send(string toUser, string message)
{
objMediator.SendMessage(toUser, this.userName, message);
}
public override void Receive(string fromUser, string message)
{
Console.WriteLine(this.userName + ":" + message);
}
}
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);
}
}
No comments:
Post a Comment