← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Mediator Pattern

quality PHP 5.0+ Intermediate

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 19
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
1 ping F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 2 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 0 pings S
No pings yet today
Amazonbot 1
Amazonbot 6 ChatGPT 3 Perplexity 2 Unknown AI 2 Ahrefs 2 Google 2 Majestic 1
crawler 16 crawler_json 2
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