← Home ← Codex ← DEBT ← Engine
Browse by Category
+ added · updated 7d
← Back to glossary

Mediator Pattern

Code Quality PHP 5.0+ Intermediate
debt(d7/e7/b7/t5)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints indicate phpstan as the tool but automated detection is explicitly marked 'no'. The code_pattern (N*M coupling mesh) is something phpstan can hint at structurally but won't flag as a mediator anti-pattern; recognising that the mediator has grown into a god class or that direct coupling should be mediated requires careful code review rather than automated tooling.

e7 Effort Remediation debt — work required to fix once spotted

Closest to 'cross-cutting refactor across the codebase' (e7). The quick_fix describes introducing a mediator when many objects communicate directly — this means touching every participating component to redirect their communication through the mediator, updating interfaces, and restructuring interaction logic. This is inherently a cross-cutting change spanning multiple files and components rather than a single-file or small-component fix.

b7 Burden Structural debt — long-term weight of choosing wrong

Closest to 'strong gravitational pull' (e7). The mediator applies_to web, cli, and queue-worker contexts and carries the decoupling tag for the whole system. Once introduced, every future feature that involves inter-component communication must be routed through or around it. The common_mistakes note about it becoming a god class confirms that it shapes how every change is made — components must coordinate via the mediator, imposing a persistent structural tax on all future work.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap (a documented gotcha most devs eventually learn)' (t5). The misconception field directly names the trap: developers introduce the mediator to reduce coupling but inadvertently concentrate all coupling in the mediator, risking a god class. This is a well-documented gotcha that most developers who use the pattern encounter, but it is not catastrophic or entirely counterintuitive — the pattern does what it advertises at the component level, the trap emerges only over time with undisciplined growth.

About DEBT scoring →

Also Known As

mediator event bus message broker pattern

TL;DR

Centralises complex communications between multiple objects into a single mediator object, reducing direct dependencies between colleagues.

Explanation

The Mediator pattern reduces chaotic many-to-many dependencies between objects by routing all communication through a central mediator. Instead of objects knowing about each other, they only know about the mediator. This reduces coupling from O(n²) to O(n). In PHP, the Command Bus (used in CQRS) is a form of mediator: commands are sent to the bus and routed to handlers, with no direct coupling between the sender and handler. Event dispatchers also act as mediators. Avoid letting the mediator become a God Object — it should route, not contain, business logic.

Common Misconception

The mediator pattern reduces coupling by centralising it. It reduces coupling between components but concentrates it in the mediator — if the mediator grows without discipline it becomes a god class that knows about every other component.

Why It Matters

The Mediator reduces direct dependencies between components by routing communication through a central object — components stay loosely coupled and the interaction logic is centralised.

Common Mistakes

  • The mediator becoming a god class that knows too much — distribute logic back to components and keep the mediator as a router.
  • Using a mediator for components that have a simple, stable relationship — adds unnecessary complexity.
  • Not distinguishing mediator (coordinates peers) from observer (notifies subscribers) — they solve different problems.
  • Tightly coupling mediator to concrete component types — use interfaces so components can be swapped.

Code Examples

✗ Vulnerable
// Direct coupling between components — every component knows every other:
class Chat {
    public function send(User $from, User $to, string $msg): void {
        $to->receive($msg, $from); // Direct coupling — use a ChatMediator
    }
}
✓ Fixed
// Without mediator: components know about each other
// With mediator: all communication goes through one hub

interface Mediator {
    public function notify(object $sender, string $event): void;
}

class DialogMediator implements Mediator {
    public function __construct(
        private Checkbox $checkbox,
        private Input    $input,
        private Button   $submitBtn,
    ) {}

    public function notify(object $sender, string $event): void {
        if ($sender === $this->checkbox && $event === 'check') {
            $this->input->setEnabled($this->checkbox->isChecked());
            $this->submitBtn->setEnabled($this->checkbox->isChecked());
        }
    }
}

Added 15 Mar 2026
Edited 22 Mar 2026
Views 102
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 1 ping W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 2 pings W 0 pings T 1 ping F 0 pings S 1 ping S 0 pings M 0 pings T 3 pings W 1 ping T 0 pings F 1 ping S 0 pings S 1 ping M
Ahrefs 1
No pings yesterday
PetalBot 11 Amazonbot 9 Ahrefs 8 ChatGPT 8 SEMrush 6 Scrapy 6 Bing 5 Google 4 Perplexity 3 Twitter/X 3 Brave Search 3 Unknown AI 2 Claude 2 Applebot 2 Majestic 1 Meta AI 1
crawler 68 crawler_json 6
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Medium
⚡ Quick Fix
When many objects communicate directly creating a mesh of dependencies, introduce a mediator — objects send messages to the mediator which routes them, they no longer know each other
📦 Applies To
PHP 5.0+ web cli queue-worker laravel symfony
🔗 Prerequisites
🔍 Detection Hints
N objects each directly referencing M other objects creating N*M coupling; changing one class cascades changes
Auto-detectable: ✗ No phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Medium Context: Class Tests: Update


✓ schema.org compliant