32 lines
1000 B
Java
32 lines
1000 B
Java
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);
|
|
}
|