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.
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
FanB is a class which does not get copies of its own messages.
public delegate void Callback
(string message, string from);
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)
protected string name;
public Fan(FacebookGroupMediator mediator,
this.mediator = mediator;
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);
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))
" received from " + from + ": " + message);
static void Main()
FacebookGroupMediator m =
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");
fan1.Send("Do you agree that this is the best group");
fan2.Send("Yes i Agree");
And out is:
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.