Behavioral Patterns  «Prev  Next»
Lesson 11Mediator: structure
ObjectiveExplore the structure of the Mediator pattern.

The Mediator Pattern: Structure and Context within the Gang of Four Design Patterns

Abstract: The Mediator Pattern, as described by the Gang of Four (GoF) in their seminal work "Design Patterns: Elements of Reusable Object-Oriented Software", addresses the common software design challenge of decoupling objects so that they can interact without being tightly bound to each other. Within the vast expanse of design patterns, the Mediator emerges as a central figure in promoting object cohesiveness and reducing system complexity. This article delineates the structural elements of the Mediator pattern, establishing its significance within the context of the Gang of Four patterns.
  1. Introduction: As systems grow in complexity, the interrelationships between objects can become a quagmire of intertwined dependencies, thereby exacerbating maintenance and reducing modularity. The Gang of Four, recognizing this challenge, introduced the Mediator pattern to abstract and encapsulate these interactions.
  2. Structural Elements of the Mediator Pattern: A cursory glance at the Mediator pattern reveals its fundamental components:
    1. Mediator: The central component that defines the interface for communication between Colleague objects. The Mediator interface is typically defined to maintain references to Colleague objects and to facilitate their interaction.
    2. ConcreteMediator: An implementation of the Mediator interface. This class cooperates with the Colleague classes to coordinate their interactions. The ConcreteMediator understands the system's logic and, thus, acts as a hub for inter-colleague communication.
    3. Colleague: A set of classes that need to communicate with each other. Instead of communicating directly, Colleagues interact with the Mediator, thereby decoupling their mutual dependencies. Each Colleague keeps a reference to its Mediator but remains oblivious of other Colleagues.
    4. ConcreteColleague: Specific implementations of the Colleague class. They communicate with the Mediator whenever they would need to interact with another Colleague.
  3. Key Principles and Benefits: The Mediator pattern's salient principle lies in the abstraction of object interactions. Instead of objects referencing each other directly, they rely on a mediator, which leads to:
    1. Decoupling: Objects remain loosely coupled, ensuring that a change in one object's behavior doesn't necessitate changes in others.
    2. Reduced Subclassing: By encapsulating object interactions within a mediator, the need for subclasses to manage these interactions is mitigated.
    3. Centralized Control: By channeling interactions through a mediator, control logic is centralized, simplifying maintenance and modifications.
  4. Context within the Gang of Four Patterns: The Gang of Four categorized design patterns into three groups: creational, structural, and behavioral. The Mediator pattern falls within the behavioral category as it primarily deals with object responsibilities and their interactions. It closely aligns with other behavioral patterns, such as Observer and Command, which also focus on decoupling system components.
  5. Conclusion: Within the architectural panorama set forth by the Gang of Four, the Mediator pattern stands as an emblematic example of the philosophy of modular design. By externalizing and centralizing object interactions, the Mediator pattern paves the way for systems that are both robust and maintainable. In a world where software systems are constantly evolving, such patterns provide the anchors of stability and clarity, guiding developers towards optimal design solutions.
The fundamental idea of the Mediator pattern is that one central Mediator object manages all connections between different objects.

Mediator pattern with 4 Front End objects and 5 Back End Objects.
Mediator pattern with 4 Front End objects and 5 Back End Objects.

In this diagram, I have represented the colleagues talking through the Mediator into frontend and backend objects. Generally, the front end would be the graphical user interface components like buttons and text boxes, and the back end would be the actual data model such as a JavaBeans. This is a common use of the Mediator pattern. This structure is more vague than most of the other patterns you have seen. There is no superclass-subclass structure and the number of objects mediated may change. Each object probably has a reference or pointer to the Mediator, but the Mediator may or may not have specific references to each of the objects. A change in a frontend object may not affect the back end. For instance, in a word processor, a change in the scroll bar may result in a corresponding change in the document view, but no change at all to the data structure for the text stored in the back end.

Mediator Pattern: The Problem

We want to define an object that encapsulates how a set of objects interact. The Mediator Pattern promotes loose coupling[1] by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
  1. The problem that commonly occurs in dialog boxes
  2. If each object in the dialog takes responsibility for the dependencies it is associated with, the result is a highly coupled set of objects with low cohesion.

Mediator Relationship

  1. Prior to the use of a mediator there is a complex relationship between all of the classes
  2. The mediator disconnects the classes and presents a single target interface or set of targets
[1]Loose Coupling: Loose coupling in object modeling is a situation where two or more objects are minimally dependent on each other. This means that a change to one object is less likely to require changes to one or more other objects.