Add all 23 GoF design pattern implementations (2026-06-13)
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/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