Mediator Pattern Code Review

Define the Colleague Objects

Code Review

Code Walkthrough

The Colleague class (object) represents the person who either sends or receives messages through the mediator.

class Colleague { Mediator m_mediator; protected String m_name;
public Colleague(Mediator mediator, String name) { this.m_mediator = mediator; mediator.SignOn(this.Receive); this.m_name = name; }
public virtual void Receive(String message, String from) { if (!String.Equals(from, this.m_name)) { Console.WriteLine("Colleague {0} received from {1}: {2}", this.m_name, from, message); } } public void Send(String message) { Console.WriteLine("Colleague Send (From {0}): {1}", this.m_name, message); this.m_mediator.Send(message, this.m_name); } }

Each instance of the Colleague class (object) will register itself with the Mediator class (object) using the SignOn() method. Messages will be send by invoking the Send() method on the Mediator class (object).

The ColleagueB class (object) is essentially another version of the Colleague class (object) and represents the person who either sends or receives messages through the mediator.

class ColleagueB : Colleague { public ColleagueB(Mediator mediator, String name) : base(mediator, name) { }
public override void Receive(string message, string from) { if (!String.Equals(from, this.m_name)) { Console.WriteLine("ColleagueB {0} received from {1}: {2}", this.m_name, from, message); } } }