Add all 23 GoF design pattern implementations
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package chain;
|
||||
|
||||
/** Handles CRITICAL tickets — all-hands incident response */
|
||||
public class CriticalIncidentTeam extends SupportHandler {
|
||||
|
||||
@Override
|
||||
protected boolean canHandle(SupportTicket ticket) {
|
||||
return ticket.getPriority() == SupportTicket.Priority.CRITICAL;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle(SupportTicket ticket) {
|
||||
System.out.println(" [CRITICAL TEAM] All-hands war room opened: " + ticket);
|
||||
}
|
||||
}
|
||||
15
03-behavioral/chain-of-responsibility/Level1Support.java
Normal file
15
03-behavioral/chain-of-responsibility/Level1Support.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package chain;
|
||||
|
||||
/** Handles LOW priority tickets — basic FAQ and documentation responses */
|
||||
public class Level1Support extends SupportHandler {
|
||||
|
||||
@Override
|
||||
protected boolean canHandle(SupportTicket ticket) {
|
||||
return ticket.getPriority() == SupportTicket.Priority.LOW;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle(SupportTicket ticket) {
|
||||
System.out.println(" [Level-1] Resolved with FAQ: " + ticket);
|
||||
}
|
||||
}
|
||||
15
03-behavioral/chain-of-responsibility/Level2Support.java
Normal file
15
03-behavioral/chain-of-responsibility/Level2Support.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package chain;
|
||||
|
||||
/** Handles MEDIUM priority tickets — technical troubleshooting */
|
||||
public class Level2Support extends SupportHandler {
|
||||
|
||||
@Override
|
||||
protected boolean canHandle(SupportTicket ticket) {
|
||||
return ticket.getPriority() == SupportTicket.Priority.MEDIUM;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle(SupportTicket ticket) {
|
||||
System.out.println(" [Level-2] Diagnosed and fixed: " + ticket);
|
||||
}
|
||||
}
|
||||
15
03-behavioral/chain-of-responsibility/Level3Support.java
Normal file
15
03-behavioral/chain-of-responsibility/Level3Support.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package chain;
|
||||
|
||||
/** Handles HIGH priority tickets — senior engineers */
|
||||
public class Level3Support extends SupportHandler {
|
||||
|
||||
@Override
|
||||
protected boolean canHandle(SupportTicket ticket) {
|
||||
return ticket.getPriority() == SupportTicket.Priority.HIGH;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void handle(SupportTicket ticket) {
|
||||
System.out.println(" [Level-3] Engineering deep-dive completed: " + ticket);
|
||||
}
|
||||
}
|
||||
38
03-behavioral/chain-of-responsibility/Main.java
Normal file
38
03-behavioral/chain-of-responsibility/Main.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package chain;
|
||||
|
||||
/**
|
||||
* Chain of Responsibility Design Pattern — Runnable Demo
|
||||
*
|
||||
* A support ticket routing system where each handler
|
||||
* either resolves a ticket or passes it up the chain.
|
||||
*
|
||||
* Run: javac chain/*.java -d out/chain && java -cp out/chain chain.Main
|
||||
* Article: https://ankurm.com/chain-of-responsibility-design-pattern-java/
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== Chain of Responsibility Demo ===\n");
|
||||
|
||||
// Build the chain: L1 -> L2 -> L3 -> Critical
|
||||
SupportHandler l1 = new Level1Support();
|
||||
l1.setNext(new Level2Support())
|
||||
.setNext(new Level3Support())
|
||||
.setNext(new CriticalIncidentTeam());
|
||||
|
||||
SupportTicket[] tickets = {
|
||||
new SupportTicket("Can't find the login button", SupportTicket.Priority.LOW),
|
||||
new SupportTicket("API returns 500 on /checkout", SupportTicket.Priority.MEDIUM),
|
||||
new SupportTicket("Database replication lag > 30s", SupportTicket.Priority.HIGH),
|
||||
new SupportTicket("Complete payment system outage", SupportTicket.Priority.CRITICAL),
|
||||
};
|
||||
|
||||
for (SupportTicket t : tickets) {
|
||||
System.out.println("Ticket: " + t);
|
||||
l1.handleRequest(t);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
System.out.println("=== Demo complete ===");
|
||||
}
|
||||
}
|
||||
31
03-behavioral/chain-of-responsibility/README.md
Normal file
31
03-behavioral/chain-of-responsibility/README.md
Normal file
@@ -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/
|
||||
31
03-behavioral/chain-of-responsibility/SupportHandler.java
Normal file
31
03-behavioral/chain-of-responsibility/SupportHandler.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package chain;
|
||||
|
||||
/**
|
||||
* Handler interface — defines the chain contract.
|
||||
* Each handler knows its next handler and can either
|
||||
* handle the request itself or pass it along.
|
||||
*/
|
||||
public abstract class SupportHandler {
|
||||
|
||||
private SupportHandler next;
|
||||
|
||||
public SupportHandler setNext(SupportHandler next) {
|
||||
this.next = next;
|
||||
return next; // fluent API: h1.setNext(h2).setNext(h3)
|
||||
}
|
||||
|
||||
// Template method: subclasses implement handle(); base manages chaining
|
||||
public final void handleRequest(SupportTicket ticket) {
|
||||
if (canHandle(ticket)) {
|
||||
handle(ticket);
|
||||
} else if (next != null) {
|
||||
System.out.println(" [" + getClass().getSimpleName() + "] passing up...");
|
||||
next.handleRequest(ticket);
|
||||
} else {
|
||||
System.out.println(" [UNHANDLED] No handler for: " + ticket);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract boolean canHandle(SupportTicket ticket);
|
||||
protected abstract void handle(SupportTicket ticket);
|
||||
}
|
||||
21
03-behavioral/chain-of-responsibility/SupportTicket.java
Normal file
21
03-behavioral/chain-of-responsibility/SupportTicket.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package chain;
|
||||
|
||||
public class SupportTicket {
|
||||
|
||||
public enum Priority { LOW, MEDIUM, HIGH, CRITICAL }
|
||||
|
||||
private final String description;
|
||||
private final Priority priority;
|
||||
|
||||
public SupportTicket(String description, Priority priority) {
|
||||
this.description = description;
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
public Priority getPriority() { return priority; }
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "[" + priority + "] " + description;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user