Mediator Design Pattern

 definition
 UML diagram
 participants
 sample code in C#



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.

Frequency of use:   medium low

UML class diagram


participants

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

  • Mediator  (IChatroom)
    • defines an interface for communicating with Colleague objects
  • ConcreteMediator  (Chatroom)
    • implements cooperative behavior by coordinating Colleague objects
    • knows and maintains its colleagues
  • Colleague classes  (Participant)
    • 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#

This structural code demonstrates the Mediator pattern facilitating loosely coupled communication between different objects and object types. The mediator is a central hub through which all interaction must take place.

Show code

// Mediator pattern -- Structural example




This real-world code demonstrates the Mediator pattern facilitating loosely coupled communication between different Participants registering with a Chatroom. The Chatroom is the central hub through which all communication takes place. At this point only one-to-one communication is implemented in the Chatroom, but would be trivial to change to one-to-many.

Show code

// Mediator pattern -- Real World example




This .NET optimized code demonstrates the same real-world situation as above but uses modern, built-in .NET features, such as, generics, reflection, object initializers, automatic properties, etc.

Show code

// Mediator pattern -- .NET optimized




   Better Code 
   Better Career 
   Better Lifestyle 


.NET
Design Pattern
Framework 4.0


 
C# and VB.NET


   Includes:

  Gang of Four Patterns
  Head First Patterns
  Enterprise Patterns
  SOA Patterns

  WPF Best Practices
  WCF Best Practices
  LINQ Best Practices

  Model View Controller
  Model View Presenter
  Model View ViewModel

  Much more...

Click here for details

-- Instant Access --
Instant Download