mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-10-17 10:14:27 +00:00
This commit is contained in:
1 parent
4108f86177
commit
1ea98bb8d5
8 files changed
+106
-86
No files matched your search
@@ -118,18 +118,24 @@ The Orc King gives the orders and forms the chain.
|
||||
|
||||
```java
|
||||
public class OrcKing {
|
||||
RequestHandler chain;
|
||||
|
||||
private List<RequestHandler> handlers;
|
||||
|
||||
public OrcKing() {
|
||||
buildChain();
|
||||
}
|
||||
|
||||
private void buildChain() {
|
||||
chain = new OrcCommander(new OrcOfficer(new OrcSoldier(null)));
|
||||
handlers = Arrays.asList(new OrcCommander(), new OrcOfficer(), new OrcSoldier());
|
||||
}
|
||||
|
||||
public void makeRequest(Request req) {
|
||||
chain.handleRequest(req);
|
||||
handlers
|
||||
.stream()
|
||||
.sorted(Comparator.comparing(RequestHandler::getPriority))
|
||||
.filter(handler -> handler.canHandleRequest(req))
|
||||
.findFirst()
|
||||
.ifPresent(handler -> handler.handle(req));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Reference in new issue
Block a user