标签:static extend void rri style string 处理 nbsp dem
职责链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合。将这些对象连成一条链,沿着这条链传递该请求,直到有一个对象处理为止。
public abstract class Handler {
protected Handler handler;
public void setHandler(Handler handler) {
this.handler = handler;
}
public abstract void handlerRequest(int stamp);
}
public class Handler_A extends Handler{
@Override
public void handlerRequest(int stamp) {
if (stamp >= 0 && stamp < 5) {
System.out.println(this.getClass().getName() + ",处理:" + stamp);
} else if (handler != null) {
handler.handlerRequest(stamp);
}
}
}
public class Handler_B extends Handler {
@Override
public void handlerRequest(int stamp) {
if (stamp > 5) {
System.out.println(this.getClass().getName() + ",处理:" + stamp);
} else if (handler != null) {
handler.handlerRequest(stamp);
}
}
}
public class ChainDemo {
public static void main(String[] args) {
Handler handlerA = new Handler_A();
Handler handlerB = new Handler_B();
handlerA.setHandler(handlerB);
int[] stamps = {0, 1, 3, 5, 9, 11, 15};
for (int stamp : stamps) {
handlerA.handlerRequest(stamp);
}
}
}
标签:static extend void rri style string 处理 nbsp dem
原文地址:http://www.cnblogs.com/emoji1213/p/7637346.html