Complete the code to declare the next handler reference in the Chain of Responsibility pattern.
class Handler { Handler [1]; }
The next handler in the chain is typically stored in a variable named nextHandler.
Complete the code to forward the request to the next handler if current handler cannot process it.
void handleRequest(Request req) {
if (canHandle(req)) {
process(req);
} else if ([1] != null) {
[1].handleRequest(req);
}
}The request is passed to the nextHandler if the current handler cannot process it.
Fix the error in the code to correctly set the next handler in the chain.
void setNextHandler(Handler [1]) {
this.nextHandler = next;
}The parameter name should match the variable used inside the method. Here, next is the parameter.
Fill both blanks to complete the method that processes the request or forwards it.
void handleRequest(Request req) {
if ([1](req)) {
[2](req);
} else if (nextHandler != null) {
nextHandler.handleRequest(req);
}
}The method canHandle checks if the handler can process the request, and process executes the handling.
Fill all three blanks to create a dictionary comprehension that maps handler names to their status if they can handle a request.
status_map = {handler.[1]: handler.[2] for handler in handlers if handler.[3](request)}The comprehension maps each handler's name to its status if it canHandle the request.
