Mediator
Learning Objectives
Centralize communication between peer objects.
Reduce many-to-many coupling without creating a god object.
Prerequisites: Modules 21, 41.
Intuition and Definition
Intent: define an object that encapsulates how a set of objects interact; colleagues refer to the mediator instead of to each other, which keeps them loosely coupled.
Participants: Mediator (knows the colleagues and the coordination rules), Colleagues (notify the mediator of events and act on its instructions).
Why It Matters
With *n* objects that all talk to each other there are up to n(n−1) links. A mediator replaces them with n links and puts the interaction rules in one place.
Syntax and Language Notes
Air-traffic control is the classic analogy: planes talk to the tower, not to each other.
Complete Example
#include <iostream>
#include <string>
class ClimateMediator;
class Colleague {
protected:
ClimateMediator& m_;
public:
explicit Colleague(ClimateMediator& m) : m_(m) {}
virtual ~Colleague() = default;
};
class Window : public Colleague {
public:
using Colleague::Colleague;
bool open = false;
void setOpen(bool o);
};
class AirCon : public Colleague {
public:
using Colleague::Colleague;
bool running = false;
void setRunning(bool r) { running = r; std::cout << " aircon " << (r ? "on" : "off") << "\n"; }
};
class Display : public Colleague {
public:
using Colleague::Colleague;
void show(const std::string& s) { std::cout << " display: " << s << "\n"; }
};
class ClimateMediator { // the interaction rules live here
public:
Window* window = nullptr; AirCon* aircon = nullptr; Display* display = nullptr;
void windowChanged(bool open) {
if (open && aircon->running) {
aircon->setRunning(false);
display->show("A/C paused: window open");
}
}
void userWantsCooling() {
if (window->open) { display->show("close the window to start A/C"); return; }
aircon->se
…aircon on
window opened
aircon off
display: A/C paused: window open
display: close the window to start A/C
window closed
aircon onStep-by-Step Walkthrough
Window doesn't know the A/C exists; it just tells the mediator it changed.
The rule "an open window pauses the A/C" lives in the mediator, not scattered between window and A/C classes.
Adding a rain sensor that closes windows touches the mediator and the new colleague only.
Common Mistakes
God mediator containing all application logic — split by interaction area.
Colleagues still talking to each other directly, bypassing the mediator.
Hidden ordering assumptions inside the mediator — document and test interaction sequences.
Trade-offs and Alternatives
Mediator centralizes control (easier to understand interactions, but a hot spot). An event bus/Observer decentralizes it (looser coupling, but rules spread across subscribers). Facade is one-directional; mediator is bidirectional.
Real-World Use
Dialog boxes coordinating widgets, chat rooms, air-traffic control systems, MVC controllers/view-models, vehicle body-control modules coordinating windows, locks and lights.
Practice
Q1. (Easy) Do colleagues reference each other in Mediator?
Answer: No — only the mediator.
Q2. (Medium) How many links does a mediator need for 10 colleagues?
Answer: 10 (one per colleague) instead of up to 90 directed links.
Q3. (Advanced) How would you keep a mediator from becoming a god object?
Answer: One mediator per interaction context (climate, doors, infotainment), keep domain rules in the colleagues and only coordination in the mediator, and test interaction scenarios explicitly.
Interview Question
Q1. "Mediator vs Observer?" Answer: Observer is a one-to-many notification mechanism: a subject broadcasts to subscribers that it doesn't know concretely; interaction rules end up spread across subscribers. Mediator is a coordinator that knows the colleagues and encapsulates the rules of how they interact; colleagues are decoupled from each other but coupled to the mediator. Mediators are often *implemented* with observer-style notifications from colleagues to the mediator.
Quick Quiz
Q1. What does the mediator encapsulate?
Answer: The interaction rules between colleagues.
Q2. Main risk?
Answer: It grows into a god object.
Q3. Classic analogy?
Answer: An air-traffic control tower.
Summary
Mediator centralizes peer interactions and replaces many-to-many links.
Keep mediators focused per context.
Related: Modules 37, 41.