Mediator Pattern

 Rate It (1)

The mediator pattern encapsulate the interaction between a set of objects.
Also it encapsulate the protocol between those objects. The pattern help 
to lose couple the object by keeping them from referring one each other.
 
Use Cases for the Mediator Pattern

You should use the pattern in the following cases:

  • Behavior that is distributed between some objects can be grouped
    or customized.
  • Object reuse is difficult because it communicates with other objects.
  • Objects in the system communicate in well-defined but complex ways.

UML Diagram 
Mediator Pattern  

Example in C#
The following code is an example of how to implement the pattern:

    #region Mediator

 

    public interface Mediator

    {

        #region Methods

 

        void Send(string message, Colleague colleague);

 

        #endregion

    }

 

    #endregion

 

    #region Concrete Mediator

 

    public class ConcreteMediator : Mediator

    {

        #region Properties

 

        public List<ConcreteColleague> Colleagues { get; private set; }

 

        #endregion

 

        #region Ctor

 

        public ConcreteMediator()

        {

            Colleagues = new List<ConcreteColleague>();

        }

 

        #endregion

 

        #region Mediator Members

 

        public void Send(string message, Colleague colleague)

        {

            foreach (Colleague currentColleague in Colleagues)

            {

                if (!currentColleague.Equals(colleague))

                {

                    currentColleague.Recieve(message);

                }

            }

        }

 

        #endregion

    }

 

    #endregion

 

    #region Colleague

 

    public abstract class Colleague

    {

        #region Members

 

        protected Mediator _mediator;

 

        #endregion

 

        #region Ctor

 

        public Colleague(Mediator mediator)

        {

            _mediator = mediator;

        }

 

        #endregion

 

        #region Methods

 

        /// <summary>

        /// Sends the given message

        /// </summary>

        /// <param name="message">The given message</param>

        public abstract void Send(string message);

 

        /// <summary>

        /// Recieves the given message

        /// </summary>

        /// <param name="message">The given message</param>

        public abstract void Recieve(string message);

 

        #endregion

    }

 

    #endregion

 

    #region Concrete Colleague

 

    public class ConcreteColleague : Colleague

    {

        #region Properties

 

        public int ID { get; set; }

 

        #endregion

 

        #region Ctor

 

        public ConcreteColleague(Mediator mediator, int id)

            : base(mediator)

        {

            ID = id;

        }

 

        #endregion

 

        #region Methods

 

        public override void Send(string message)

        {

            _mediator.Send(message, this);

        }

 

        public override void Recieve(string message)

        {

            Console.WriteLine("{0} recieved the message: {1}",

                ID, message);

        }

 

        #endregion

    }

 

    #endregion

As can be seen in the example, I have a mediator object that sends messages
to the concretes of the colleague class. The example is simple but it shows the
concepts that are used in the mediator pattern. With few modifications you
can use the example to build a small chat room application.
The following code was used to test the previous example:

   ConcreteMediator mediator = new ConcreteMediator();

 

   ConcreteColleague colleague1 = new ConcreteColleague(mediator, 1);

   ConcreteColleague colleague2 = new ConcreteColleague(mediator, 2);

 

   mediator.Colleagues.Add(colleague1);

   mediator.Colleagues.Add(colleague2);

 

   colleague1.Send("Hello from colleague 1");

   colleague2.Send("Hello from colleague 2");

 

   Console.Read();

Summary
To sum up, I explained the use of the mediator pattern.
When considering to use the mediator pattern you should always remember
that the patten has a performance impact. The performance impact is caused because every
communications pass through the mediator on the way to the
communicated object – which make the mediator a bottle neck in your system. If you don’t
want to use the pattern I suggest the facade pattern instead to wrap your subsystem.

Revision number 1, Thursday, October 02, 2008 9:09:21 PM by gilfink

Comments

Related Articles

Design Patterns

Design Patterns Design patterns are recognized solutions to common problems defined originally by the Gang of Four programmers. Design patterns are used throughout the ASP.NET Framework. The various patterns are commonly divided into several different groups

Prototype Pattern

The prototype is built upon the use of object cloning. The prototype creates new objects by cloning one of its concrete classes. The prototype is used in the following situations: You need to hide the concrete product classes from the client. You want to reduce

Threat Modeling

It's absolutely necessary if you're serious about security. Whitepapers/Books/Blogs Threat Modeling for ASP.NET (PDF) - an excellent white paper from Rüdiger Grimm and Henrik Eichstädt from the University of Kent Threat Modeling book from MSPress

IDisposable Pattern

The IDisposable pattern isn't one of the a classic patterns. It's a pattern suggested in MSDN to implement the IDisposable interface. You should be familiar with the pattern or with the interface because it's a basic thing to know about the .Net

Shortcuts

Table of Contents

Top Wiki Contributors

(last 30 days)

  1. mbanavige (5)
  2. SGWellens (4)
  3. maartenba (2)
  4. rami_nassar (2)
  5. stiansol (2)
  6. MisterFantastic (2)
  7. satish1.v (1)
  8. raklos (1)
  9. mosessaur (1)
  10. Jos Branders (1)

Advertise Here

Microsoft Communities
Page view counter