Mediator Pattern
Collect interaction rules in a mediator so collaborating objects do not depend directly on each other.
· Last updated: 2026-11-07 · 9 min read
Overview
On an input form, a submit button may be enabled only when all input fields are complete and a confirmation checkbox is checked. Once the checkbox starts referring directly to the button or to other input fields, a change to a single screen forces you to touch code that has become tangled together. To find out why the button is on, you have to read the code of several elements side by side.
You get rid of that direct reference by gathering the interactions between objects in one place. The intermediary that coordinates them is called a Mediator. Each element reports only its own change, and the mediator decides how the other elements should change because of it.
UML
IFormMediator does one thing: it relays the fact that a value changed. Toggle reports its change to that channel, and FormMediator is what combines the two conditions and sets SubmitButton.Enabled. No path leads straight from Toggle to the button.
History
GoF cataloged Mediator as a behavioral pattern in Design Patterns in 1994. A separate object encapsulates interactions among collaborating objects.1
Why use it?
The mediator receives change notifications from the controls, checks the submit condition and updates the button. Individual controls need not know one another's concrete types or the coordination rules. Changing the submit condition means changing the mediator's rule.
A mediator that takes on work beyond the screen as well soon becomes a large manager object. This example stops at coordinating the state of one form, and it does not take the place of the input validation or the authorization that the server performs. The code in the next section stays inside that boundary.
Example
The example shows where the two conditions come together. The button turns on only when both the completed input and the confirmation check are true, and Toggle here is not a real UI control but a stand-in that sends nothing but a change notification.
The two conditions arrive in separate notifications. FormMediator keeps the most recently reported values in details and confirmed. The first Set(true) changes only details; combining it with the still-false confirmed leaves the button disabled.
When the second notification sets confirmed to true, the mediator combines it with the stored details=true and enables the button. The first Toggle never reads the second Toggle or enables the button directly. The mediator remembers and combines their notifications, so the two widgets do not need references to each other.
using System;
using System.Collections.Generic;
using System.Linq;
var button = new SubmitButton();
IFormMediator mediator = new FormMediator(button);
new Toggle("details", mediator).Set(true);
Console.WriteLine(button.Enabled);
new Toggle("confirmed", mediator).Set(true);
Console.WriteLine(button.Enabled);
sealed class SubmitButton { public bool Enabled { get; set; } }
interface IFormMediator { bool Changed(string field, bool ready); }
// Compute relationships among widgets in this mediator.
sealed class FormMediator(SubmitButton button) : IFormMediator
{
private bool details;
private bool confirmed;
public bool Changed(string field, bool ready)
{
switch (field)
{
case "details": details = ready; break;
case "confirmed": confirmed = ready; break;
default: return false;
}
button.Enabled = details && confirmed;
return true;
}
}
// The widget reports its change without controlling another widget.
sealed class Toggle(string field, IFormMediator mediator)
{
public bool Set(bool ready) => mediator.Changed(field, ready);
}
False
TrueRunning the example shows the order. While only the first condition is true, the button stays off, and it turns on once the second condition comes in as well.
An extended example
This time one condition is set back to false, and an unknown field name is sent once as well.
using System;
using System.Collections.Generic;
using System.Linq;
var button = new SubmitButton();
IFormMediator mediator = new FormMediator(button);
mediator.Changed("details", true);
mediator.Changed("confirmed", true);
Console.WriteLine(button.Enabled);
mediator.Changed("details", false);
Console.WriteLine(button.Enabled);
Console.WriteLine(mediator.Changed("unknown", true));
Console.WriteLine(button.Enabled);
sealed class SubmitButton { public bool Enabled { get; set; } }
interface IFormMediator { bool Changed(string field, bool ready); }
// Compute relationships among widgets in this mediator.
sealed class FormMediator(SubmitButton button) : IFormMediator
{
private bool details;
private bool confirmed;
public bool Changed(string field, bool ready)
{
switch (field)
{
case "details": details = ready; break;
case "confirmed": confirmed = ready; break;
default: return false;
}
button.Enabled = details && confirmed;
return true;
}
}
// The widget reports its change without controlling another widget.
sealed class Toggle(string field, IFormMediator mediator)
{
public bool Set(bool ready) => mediator.Changed(field, ready);
}
True
False
False
FalseReleasing a condition turns the button off as well, and an unknown field is simply rejected with false while the existing state stays untouched. Rejecting a notification leaves the state already in place alone. Whereas Observer separates the notification relationships, the focus here is on how several elements are coordinated once a notification arrives.
TypeScript example
This example repeats the same split. All each Toggle reports is the fact that its value changed, and FormMediator takes charge of the relationship between the two conditions that enable the button. The split is meant to reduce the coupling in which one toggle had to know the other toggle and even the internal state of the button.
class SubmitButton { enabled = false; }
interface IFormMediator { changed(field: string, value: boolean): boolean; }
// Compute relationships among widgets in this mediator.
class FormMediator implements IFormMediator {
private details = false;
private confirmed = false;
constructor(private readonly button: SubmitButton) {}
changed(field: string, value: boolean): boolean {
if (field === "details") this.details = value;
else if (field === "confirmed") this.confirmed = value;
else return false;
this.button.enabled = this.details && this.confirmed;
return true;
}
}
// The widget reports its change without controlling another widget.
class Toggle {
constructor(private readonly field: string, private readonly mediator: IFormMediator) {}
set(value: boolean): boolean { return this.mediator.changed(this.field, value); }
}
const button = new SubmitButton();
const mediator = new FormMediator(button);
new Toggle("details", mediator).set(true);
console.log("after details: " + button.enabled);
new Toggle("confirmed", mediator).set(true);
console.log("after confirmation: " + button.enabled);
console.log("unknown accepted: " + mediator.changed("unknown", false));
console.log("still enabled: " + button.enabled);after details: false
after confirmation: true
unknown accepted: false
still enabled: trueAn unknown field is rejected with false, and the button state is left alone. Because the example deals only with UI coordination, the server validation and authorization that a real submission needs lie outside this code. That way the mediator's scope stays inside the screen.
Boundaries with Facade and Observer, and avoiding the God Mediator
Three patterns often sit side by side here, so their boundaries are worth pinning down. A Facade Pattern simplifies a subsystem for external callers in a one-way direction; the subsystem classes inside never know the facade exists. A Mediator sits among peers (colleagues), replacing an mesh of direct references with an -spoke star topology and coordinating them multidirectionally. And whereas the Observer Pattern provides the general mechanism for broadcasting state changes to anonymous listeners, a Mediator encapsulates the specific coordination policy—such as enabling the submit button only when both the text field and confirmation toggle are satisfied.
The classic failure mode of Mediator is trading tangled peer dependencies for a monolithic God Object. Every time a new widget rule or business check gets dumped into the mediator, its state space balloons. Keep individual field formatting inside the colleagues, restrict the mediator strictly to cross-component coordination, and when state transitions outgrow a handful of booleans, replace ad-hoc mutable flags in the mediator with an explicit State Pattern or unidirectional MVU Pattern reducer.
Related reading
Footnotes
- Gamma, Helm, Johnson, Vlissides. Design Patterns (1994). Publisher record. Behavioral patterns, Mediator. ↩