Click here to Skip to main content
11,429,154 members (60,799 online)
Click here to Skip to main content
Technical Blog

Mediator design pattern

, 20 Jul 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
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.


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 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;

        // Send is implemented as a broadcast
        public void Send(string message, string from)
            respond(message, from);

    class Fan
        FacebookGroupMediator mediator;
        protected string name;

        public Fan(FacebookGroupMediator mediator, 
            string name)
            this.mediator = mediator;
   = 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)

        // Does not get copies of own messages
        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");
        // this is the fan who does not want to get copy own messages
        fan2.Send("yes i also like this group");
        // fan 3 temp uncheck to get the messages
        fan1.Send("Do you agree that this is the best group");
        fan2.Send("Yes i Agree");
        // open again
        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.

Difference between Observer Design Pattern in C# and Mediator Design Pattern

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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) BMJ
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

Questionany way to do this in C++? Pin
Alex Ten19-Apr-12 20:32
memberAlex Ten19-Apr-12 20:32 
GeneralMy vote of 1 Pin
Marek Konieczny22-Jul-09 4:29
memberMarek Konieczny22-Jul-09 4:29 
GeneralRe: My vote of 1 Pin
Salmanzz31-Jul-09 6:42
memberSalmanzz31-Jul-09 6:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 20 Jul 2009
Article Copyright 2009 by Salmanzz
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid