Mediator: sync post code blocks to repo exactly, Java 25, add H6 file labels, full console output, README mapping

This commit is contained in:
2026-06-24 16:45:37 +05:30
parent 7b156a7c50
commit 78a99c4f96
2 changed files with 48 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
# Mediator Design Pattern — Java Example
**Pattern:** Behavioral → Mediator
**Article:** https://ankurm.com/mediator-design-pattern-java/
## What this example shows
A chat room where users never talk to each other directly. `ChatMediator` declares the hub contract (`sendMessage()`/`addUser()`). `User` is a colleague — it knows only the mediator, never other users. `ChatRoom` is the concrete mediator: it holds the list of users and routes every message to everyone except the sender. `Main` adds three users and has each one broadcast a message, then prints the O(N) vs O(N²) connection-count comparison.
## How to run
```bash
javac mediator/*.java -d out/mediator
java -cp out/mediator mediator.Main
```
Requires Java 25.
## Post Section ↔ File Mapping
| Post Section | File(s) |
|---|---|
| Implementation: Chat Room — the Mediator interface | `ChatMediator.java` |
| Implementation: Chat Room — the Colleague | `User.java` |
| Implementation: Chat Room — the Concrete Mediator | `ChatRoom.java` |
| Implementation: Chat Room — wiring it together | `Main.java` |
Note: the "Mediator in Practice: MVC and Spring Events" snippet (`ApplicationEventPublisher`/`@EventListener` example) is illustrative only — it is not part of this repository's runnable example.
Article: https://ankurm.com/mediator-design-pattern-java/
All patterns: https://ankurm.com/design-patterns-java/