Table of Contents

Introduction

The Mediator is a behavioral design pattern that provides a central hub to guide the interactions between many objects. According to Gamma et al, the intent of the Mediator pattern is to,

"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." (Gamma et al., 1995)

You will find on this page, a detailed, and hopefully useful, investigation into the various aspects of this relatively simple, yet powerful design pattern.

Back to top.

Air Traffic Controller Example

Before we explore the Mediator pattern in any depth, first consider the example of an air traffic controller, and the various aircraft it helps to guide (Bains K., Lau E., 2002).

Imagine what would happen if there were no air traffic controllers. Each and every aircraft would have to know about every other aircraft in order to avoid collisions with them. This would certainly be a cause for disaster, as it would be far too difficult for each plane to keep track of all other aircraft in the vicinity.

So, to remedy this, we have the air traffic controller. Instead of having all aircraft communicating directly with one another, they simply send the controller their flight data and the controller then decides what other aircraft need to be informed. This is clearly far more efficient, and in this case, much safer, than the alternative. This is a perfect example of the Mediator pattern in the real-world.

Back to top.

Structure

Expressed in UML, a Mediator pattern may be structured with a handful of simple classes and interfaces (Figure 1.).

Figure 1: Mediator Structure

So an example implementation, in the context of the air traffic controller described earlier, could also be structured in UML (Figure 2.).

Figure 2: Air Traffic Controller Example Structure

The sequence of events that may occur between the objects involved in our air traffic controller example, can be expressed using a sequence diagram (Figure 3.).

Figure 3: Air Traffic Controller Example Sequence Diagram

More or less, this is how the Mediator design pattern works. Colleagues inform the Mediator of particular events, and the Mediator then decides what other colleagues should be informed.

Back to top.

Participants

There are three participants in the Mediator pattern (Figure 1.):

Back to top.

Applicability

According to Gamma et al, the Mediator pattern should be used when (Gamma et al., 1995):

Back to top.

Consequences

As with most design patterns, there are both advantages and disadvantages to using the Mediator. The following section will briefly outline a few of these issues.

Advantages

Disadvantages

Back to top.

Examples

Parliament

The House of Commons in the Parliaments of Canada, England and many more countries are also excellent real-life examples of the mediator pattern. According to protocols established hundreds of years ago, Members of Parliament (Colleagues) may never address each other directly. All comments are instead directed to the Speaker of the House (the Mediator). Further, it is a breach of protocol to refer to a member by name. All members are referred to as "The Honourable Member from [district]" or "My good friend from [district]." This is analogous to addressing Colleague classes by their "interface" and not by their actual "implementation".

Chatroom

To illustrate a possible use of the Mediator pattern, we have chosen to present an example chatroom application (Data & Object Factory, 2004) in Java. In this particular example, the Forum acts as the abstract Mediator, and the PoliticalForum is the concrete implementation of it. We then have a couple of Colleague implementations: MemberOfParliament, and PrimeMinister (PrimeMinister is actually a subclass of MemberOfParliament, so it too is a Colleague). By having each Colleague aware of the Forum they're participating in, they can send messages without actually referencing one another.

Back to top.

Related Patterns

A few other design patterns are closely related to the Mediator pattern, and are often used in conjunction with it.

Back to top.

Known Uses

In the following sections, we'll discuss some real-world uses of the Mediator pattern. You'll find the Mediator in many situations where there are many components that must interact with one another in complex ways.

User Interfaces

Perhaps the most common use for the Mediator pattern we've come across, is that in graphical user interfaces (Gamma et al., 1995), where a change in one control, may change the state of other controls. Instead of having the instantiating control know about all other possible controls that may need to know about changes, we

Java Message Service

Another use for the Mediator pattern is in messaging services between remote applications. For instance, the J2EE Java Message Service (Armstrong et al., 2004) provides applications with the ability to subscribe, and publish data to other applications in an asynchronous manner. Clearly, this is a variation of the Mediator, as it incorporates the Observer pattern for providing the publish and subscribe functionality, but this is a very common variation, and is worth mentioning.

Back to top.

Previous Experience

Our most common encounter with the Mediator pattern thus far has surely been in writing user interfaces; a task that dictates the use of many objects that often need to communicate with one another.

As mentioned earlier, the Mediator is well suited for coordinating changes between related controls in graphical user interfaces, and we would tend to agree. Instead of placing the code that deals with 'notifying' other controls of changes within the controls themselves, we've often found ourselves creating 'notification' methods within the container class, such as notifyFontChange(), that actually execute the various methods required to update the appropriate controls. This has helped us to reduce duplication of code, made changes to the UI far less painful, and made the code itself far easier to understand.

Conclusion

It would be difficult for us to deny the sheer simplicity and usefulness of the Mediator pattern. By providing the loose coupling between Colleague objects and centralizing the interaction logic in the Mediator, changes are more easily facilitated, and code clarity is heightened.

While we haven't encountered this yet ourselves, we can easily foresee Mediator implementations becoming very large and complex when it must deal with a very large or unknown set of Colleagues. In such cases, we feel that using the Observer pattern would actually help a great deal, by placing the interaction logic back into the Colleagues. Colleagues in this case would then publish and subscribe to events that affect them, and would deal these events themselves. The Mediator would then only provide the means by which Colleagues may inform one another of such events. Such a Mediator is actually provided by the Java Message Service, as we mentioned earlier.

References

Armstrong, E., Ball, J., Bodoff, S., Carson, D., Evans, I., Green, D., Haase, K., Jendrock, E. (August 27, 2004). The J2EE 1.4 Tutorial. http://java.sun.com/j2ee/1.4/docs/tutorial/doc/index.html. Sun Microsystems.
Bains K., Lau E. (2002). Mediator Design Pattern. http://sern.ucalgary.ca/courses/SENG/443/W02/assignments/Mediator/. University of Calgary.
Black, S. (2004). Mediator Design Pattern. http://stevenblack.com/PTN-Mediator.ASP. Steven Black Consulting.
Data & Object Factory. (2004). Design Patterns: Mediator. http://www.dofactory.com/patterns/PatternMediator.aspx. Data & Object Factory.
Gamma, E., Richard H., Johnson, R., Vlissides, J. (1995). Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley.

Back to top.

Contact Information

Please feel free to contact one of us should you have any questions about the Mediator design pattern:

You may also download a copy of this website, and related materials.

Back to top.