Mediator Pattern
The Mediator Pattern is a behavioral design pattern that centralizes communication between a set of interacting objects into a single mediator object, so those objects no longer reference each other directly and instead only talk to the…
Definition
The Mediator Pattern is a behavioral design pattern that centralizes communication between a set of interacting objects into a single mediator object, so those objects no longer reference each other directly and instead only talk to the mediator.
Overview
The Mediator Pattern addresses the problem of tightly coupled, many-to-many communication between objects, which the Gang of Four's "Design Patterns" describes as a common source of tangled dependencies as a system grows: each object that needs to notify or react to several others ends up holding direct references to all of them, creating a dense web of interdependencies that is hard to reason about, test, or modify. The Mediator Pattern breaks this web by introducing a single mediator object that all the participating (colleague) objects communicate through; a colleague notifies the mediator of an event, and the mediator decides which other colleagues need to be informed and how, effectively centralizing the interaction logic that would otherwise be scattered across every colleague class. A classic illustrative example is a graphical dialog box with multiple interdependent widgets — a checkbox that enables/disables a text field, a dropdown that filters the options in another dropdown, a submit button that becomes active only when all required fields are valid. Rather than each widget holding references to every other widget it might affect, each widget only knows about the mediator (the dialog itself), and the dialog's mediator logic decides how a change in one widget cascades to the others. This keeps each individual widget class simple and reusable, while the coordination complexity is concentrated in one place. The Mediator Pattern is closely related to, and often compared with, the Observer pattern and the broader publish-subscribe architecture; many chat room systems, air traffic control simulations, and UI frameworks use mediator-like objects to coordinate many participants without them holding direct references to each other. The main risk is that the mediator itself can become a large, complex "god object" if too much logic accumulates in it, so well-designed mediators are usually kept narrowly scoped to coordination logic, with the underlying business logic still living in the colleague objects themselves.
Key Concepts
- Centralizes many-to-many object communication into a single mediator object
- Colleague objects reference only the mediator, not each other
- Decouples interacting objects, simplifying testing and reuse
- Commonly used to coordinate interdependent UI widgets/components
- Reduces the number of direct dependencies in a subsystem
- Related to, but distinct from, the Observer and Facade patterns
- Risk of the mediator becoming an overly complex 'god object' if misused
- Coordination logic lives centrally rather than scattered across colleagues