# Handler.java
public abstract class Handler {
protected Handler nextHandler;
public void setNextHandler(Handler handler) {
this.nextHandler = handler;
}
public abstract void processRequest(Request request);
}
# Request.java
public class Request {
private int requestValue = 3;
public int getRequest() {
return requestValue;
}
public void setRequest(int value) {
this.requestValue = value;
}
}
# ProcessZero.java
public class ProcessZero extends Handler {
@Override
public void processRequest(Request request) {
if (request.getRequest() == 0)
System.out.println("request value is zero.");
else
nextHandler.processRequest(request);
}
}
# ProcessPlus.java
public class ProcessPlus extends Handler {
@Override
public void processRequest(Request request) {
if (request.getRequest() > 0)
System.out.println("request value is plus.");
else
nextHandler.processRequest(request);
}
}
# ProcessMinus.java
public class ProcessMinus extends Handler {
@Override
public void processRequest(Request request) {
if (request.getRequest() < 0)
System.out.println("request value is minus.");
else
nextHandler.processRequest(request);
}
}
# Main.java
public class Main {
public static void main(String[] args) {
Request request = new Request();
ProcessZero processZero = new ProcessZero();
ProcessMinus processMinus = new ProcessMinus();
ProcessPlus processPlus = new ProcessPlus();
processZero.setNextHandler(processMinus);
processMinus.setNextHandler(processPlus);
request.setRequest(0);
processZero.processRequest(request);
request.setRequest(1);
processZero.processRequest(request);
request.setRequest(-1);
processZero.processRequest(request);
}
}
# 결과
request value is zero.
request value is plus.
request value is minus.
댓글 영역