65.9K
CodeProject is changing. Read more.
Home

Mediator Pattern

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Oct 2, 2008

CPOL

1 min read

viewsIcon

13060

The mediator pattern encapsulate the interaction between a set of objects. Also it encapsulate the protocol between those objects. The pattern help 

Introduction

The mediator pattern encapsulate the interaction between a set of objects. Also it encapsulate the protocol between those objects. The pattern help  to loosely 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:

  • Behaviour 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

Example in C#

The following code is an example of how to implement the pattern:

    #region Mediator

    public interface IMediator
    {
        #region Methods
        void Send(string message, Colleague colleague);
        #endregion
    }

    #endregion

    #region Concrete Mediator

    public class ConcreteMediator : IMediator
    {
        #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 IMediator _mediator;
        #endregion

        #region Ctor
        public Colleague(IMediator 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(IMediator 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 façade pattern instead to wrap your subsystem.