• français (France)
    • English (United States)
    • العربية (مصر)
    • Deutsch (Deutschland)
    • Español (España, alfabetización internacional)
    • हिंदी (भारत)
    • italiano (Italia)
    • 日本語 (日本)
    • 한국어 (대한민국)
    • Nederlands (Nederland)
    • polski (Polska)
    • русский (Россия)
    • ไทย (ไทย)
    • Türkçe (Türkiye)
    • Tiếng Việt (Việt Nam)
    • 中文(中华人民共和国)
    • 中文(香港特別行政區)
  • Connexion
  • Registre
DotNetAge - Mvc & jQuery CMS
Masquer la barre latérale

Mediator


Definition



Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

Summary



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 class diagram





Participants



The classes and/or objects participating in this pattern are:

  • Mediator - defines an interface for communicating with Colleague objects
  • ConcreteMediator
implements cooperative behavior by coordinating Colleague objects

knows and maintains its colleagues

  • Colleague classes
each Colleague class knows its Mediator object

each colleague communicates with its mediator whenever it would have otherwise communicated with another colleague



Sample code in C#




  1. 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 Colleagues { get; private set; }

#endregion

#region Ctor

public ConcreteMediator()
{
Colleagues = new List();
}

#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

///
/// Sends the given message
///
/// The given message
public abstract void Send(string message);

///
/// Recieves the given message
///
/// The given message
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();

 


    Average:
  • Lectures
    (1115)
  • (0)
  • Permalien
Partager à:

Commentaires (0)


  • rss
  • atom

Il n'y a aucun commentaire dans cet article.

Tag cloud

Anything in here will be replaced on browsers that support the canvas element