-
-
Notifications
You must be signed in to change notification settings - Fork 26.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chain of Responsibility Pattern in Java: Building Robust Request Handling Mechanisms
- Loading branch information
1 parent
8bd3175
commit ac05247
Showing
1 changed file
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
@Getter | ||
public class Request { | ||
|
||
private final RequestType requestType; | ||
private final String requestDescription; | ||
private boolean handled; | ||
|
||
public Request(final RequestType requestType, final String requestDescription) { | ||
this.requestType = Objects.requireNonNull(requestType); | ||
this.requestDescription = Objects.requireNonNull(requestDescription); | ||
} | ||
|
||
public void markHandled() { | ||
this.handled = true; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getRequestDescription(); | ||
} | ||
} | ||
|
||
public enum RequestType { | ||
DEFEND_CASTLE, TORTURE_PRISONER, COLLECT_TAX | ||
} | ||
|
||
public interface RequestHandler { | ||
|
||
boolean canHandleRequest(Request req); | ||
|
||
int getPriority(); | ||
|
||
void handle(Request req); | ||
|
||
String name(); | ||
} | ||
|
||
@Slf4j | ||
public class OrcCommander implements RequestHandler { | ||
@Override | ||
public boolean canHandleRequest(Request req) { | ||
return req.getRequestType() == RequestType.DEFEND_CASTLE; | ||
} | ||
|
||
@Override | ||
public int getPriority() { | ||
return 2; | ||
} | ||
|
||
@Override | ||
public void handle(Request req) { | ||
req.markHandled(); | ||
LOGGER.info("{} handling request \"{}\"", name(), req); | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return "Orc commander"; | ||
} | ||
} | ||
|
||
// OrcOfficer and OrcSoldier are defined similarly as OrcCommander ... | ||
|
||
|
||
public class OrcKing { | ||
|
||
private List<RequestHandler> handlers; | ||
|
||
public OrcKing() { | ||
buildChain(); | ||
} | ||
|
||
private void buildChain() { | ||
handlers = Arrays.asList(new OrcCommander(), new OrcOfficer(), new OrcSoldier()); | ||
} | ||
|
||
public void makeRequest(Request req) { | ||
handlers | ||
.stream() | ||
.sorted(Comparator.comparing(RequestHandler::getPriority)) | ||
.filter(handler -> handler.canHandleRequest(req)) | ||
.findFirst() | ||
.ifPresent(handler -> handler.handle(req)); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
|
||
var king = new OrcKing(); | ||
king.makeRequest(new Request(RequestType.DEFEND_CASTLE, "defend castle")); | ||
king.makeRequest(new Request(RequestType.TORTURE_PRISONER, "torture prisoner")); | ||
king.makeRequest(new Request(RequestType.COLLECT_TAX, "collect tax")); | ||
} |