Introduction
The Mediator pattern is there to enable objects to communicate without knowing each other’s identities. It promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
Explanation
Mediator design pattern deals with communication. Today there could be many ways of communication. Consider a Facebook group where so many people become fans and get messages so people interested in that group can get messages by subscribing to it. Any messages that come in will go to all of the list’s subscribers. Members can remove themselves from the list at any time by going to their notification options or from their group preferences. So clearly the moderation process here is performing part of the role of the Mediator pattern establishing a communication protocol and content filter. The other part keeping fans unaware of each other is inherently part of the mailing list.
FacebookGroupMediator is playing a role of mediator which has all the preferences or filter option of sign on, block or unblock, etc.
Fan is a class which sends and receives messages to that group or in any mailing list. And that class associates itself to FacebookGroupMediator.
FanB is a class which does not get copies of its own messages.
class FacebookGroupMediator
{
public delegate void Callback
(string message, string from);
Callback respond;
public void SignOn(Callback method)
{
respond += method;
}
public void Block(Callback method)
{
respond -= method;
}
public void Unblock(Callback method)
{
respond += method;
}
public void Send(string message, string from)
{
respond(message, from);
Console.WriteLine();
}
}
class Fan
{
FacebookGroupMediator mediator;
protected string name;
public Fan(FacebookGroupMediator mediator,
string name)
{
this.mediator = mediator;
mediator.SignOn(Receive);
this.name = name;
}
public virtual void Receive(string message, string from)
{
Console.WriteLine(name + " received from "
+ from + ": " + message);
}
public void Send(string message)
{
Console.WriteLine("Send (From "
+ name + "): " + message);
mediator.Send(message, name);
}
}
class FanB : Fan
{
public FanB(FacebookGroupMediator mediator, string name)
: base(mediator, name)
{
}
public override void Receive(string message, string from)
{
if (!String.Equals(from, name))
Console.WriteLine(name +
" received from " + from + ": " + message);
}
}
static void Main()
{
FacebookGroupMediator m =
new FacebookGroupMediator();
Fan fan1 = new Fan(m, "Fan 1");
FanB fan2 = new FanB(m, "Fan 2");
Fan fan3 = new Fan(m, "Fan 3");
fan1.Send("i like this group");
fan2.Send("yes i also like this group");
m.Block(fan3.Receive);
fan1.Send("Do you agree that this is the best group");
fan2.Send("Yes i Agree");
m.Unblock(fan3.Receive);
fan1.Send("Thanks all");
}
And out is:
So when fan1 posts a message other fans receive the message. If fan3 unchecks its receiving option, then fan3 does get any updates regarding messages.
In my previous article, Observer Design Pattern in C#, I have explained an example of blog and their subscribers. Mediator and Observer are very similar patterns. Mailing lists, chat rooms, groups and blogs, etc. all involve communication between individuals who actively sign up to the activity. The difference Mediator centralizes the communication between fans, class workers, colleagues, whereas the Observer distributes control of information by differentiating between subjects (publishers) and observers (subscribers). Mediator hides all activities that it implements, the user is better able to understand the system and the interactions that the fans make.
The communication in the Mediator pattern is easier to understand. Fans send messages to a mediator and the transmission of the information further to whoever is currently in the group is handled there, whereas in the Observer pattern do not send any information; they wait to receive it from subjects, who distributed it to all who have subscribed. An example is a blog. For further details, read Observer Design Pattern in C#.
View this article on my blog.