Files
design-patterns/03-behavioral/chain-of-responsibility/README.md

32 lines
1.6 KiB
Markdown

# Chain of Responsibility Design Pattern — Java Example
**Pattern:** Behavioral → Chain of Responsibility
**Article:** https://ankurm.com/chain-of-responsibility-design-pattern-java/
## What this example shows
A support-ticket routing system: four handlers (`Level1Support`, `Level2Support`, `Level3Support`, `CriticalIncidentTeam`) are chained together via `SupportHandler.setNext()`. Each handler checks `canHandle()` for the ticket's priority — if it can't handle the ticket, it logs that it's passing the ticket up and delegates to the next handler in the chain. The sender (`Main`) only ever talks to the first handler; it has no idea which handler will ultimately resolve each ticket.
## How to run
```bash
javac chain-of-responsibility/*.java -d out/chain
java -cp out/chain chain.Main
```
Requires Java 25.
## Post Section ↔ File Mapping
| Post Section | File(s) |
|---|---|
| The Problem: Rigid Routing Logic | illustrative only — not part of this repository's runnable example |
| Implementation: Support Ticket Escalation — base handler | `SupportHandler.java` |
| Implementation: Support Ticket Escalation — the ticket | `SupportTicket.java` |
| Each concrete handler checks only its own responsibility — Level 1 & 2 | `Level1Support.java`, `Level2Support.java` |
| Level3Support and CriticalIncidentTeam follow exactly the same shape | `Level3Support.java`, `CriticalIncidentTeam.java` |
| The chain is assembled in one place | `Main.java` |
Article: https://ankurm.com/chain-of-responsibility-design-pattern-java/
All patterns: https://ankurm.com/design-patterns-java/