Click here to Skip to main content
Click here to Skip to main content

Mediator design pattern

, 20 Jul 2009
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.

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

        // Send is implemented as a broadcast
        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)
        {
        }

        // 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
        m.Block(fan3.Receive); 
        fan1.Send("Do you agree that this is the best group");
        fan2.Send("Yes i Agree");
        m.Unblock(fan3.Receive); 
        // open again
        fan1.Send("Thanks all");
    }   

And out is:

image_4_.png

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.

License

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

Share

You may also be interested in...

About the Author

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

Comments and Discussions

 
Questionany way to do this in C++? PinmemberAlex Ten19-Apr-12 19:32 
GeneralMy vote of 1 PinmemberMarek Konieczny22-Jul-09 3:29 
Seem to be little modification of an example from Judith Bishop book "C# 3.0 Design Patterns".
GeneralRe: My vote of 1 PinmemberSalmanzz31-Jul-09 5: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 | Mobile
Web01 | 2.8.140827.1 | Last Updated 20 Jul 2009
Article Copyright 2009 by Salmanzz
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid