Add all 23 GoF design pattern implementations (2026-06-13)
This commit is contained in:
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);
|
||||
}
|
||||
Reference in New Issue
Block a user