Skip to content

Commit

Permalink
Create App.java
Browse files Browse the repository at this point in the history
Chain of Responsibility Pattern in Java: Building Robust Request Handling Mechanisms
  • Loading branch information
Dibyajyoti1515 authored Oct 5, 2024
1 parent 8bd3175 commit ac05247
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions localization/zh/chain/App.java
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"));
}

0 comments on commit ac05247

Please sign in to comment.