diff --git a/03-behavioral/chain-of-responsibility/README.md b/03-behavioral/chain-of-responsibility/README.md new file mode 100644 index 0000000..d2eb92d --- /dev/null +++ b/03-behavioral/chain-of-responsibility/README.md @@ -0,0 +1,31 @@ +# 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/ diff --git a/README.md b/README.md index e3b6da6..832949d 100644 --- a/README.md +++ b/README.md @@ -305,6 +305,23 @@ javac 02-structural/proxy/*.java -d out/proxy java -cp out/proxy proxy.Main ``` +### Chain of Responsibility (`03-behavioral/chain-of-responsibility/`) + +| 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` | + +Run it: +```bash +javac 03-behavioral/chain-of-responsibility/*.java -d out/chain +java -cp out/chain chain.Main +``` + ## Reference - *Design Patterns: Elements of Reusable Object-Oriented Software* — Gamma, Helm, Johnson, Vlissides