标签:
当多个对象都存在处理请求的情况时,通过构造一条处理责任链,将请求者和处理者解耦。这样具体的处理方式和处理顺序都可以灵活调整。
代码如下:
public abstract class Handler { public Handler next; public Handler setNext(Handler handler){ this.next = handler; return this; } public abstract void doAction(); }
public class HandlerOneImpl extends Handler { @Override public void doAction() { if(null != this.next){ this.next.doAction(); } System.out.println("HandlerOneImpl"); } }
public class HandlerTwoImpl extends Handler { @Override public void doAction() { if(null != this.next){ this.next.doAction(); } System.out.println("HandlerTwoImpl"); } }
public class App { public static void main(String[] args) { HandlerOneImpl one = new HandlerOneImpl(); one.setNext(new HandlerTwoImpl()).doAction(); } }
HandlerTwoImpl
HandlerOneImpl
标签:
原文地址:http://www.cnblogs.com/Fredric-2013/p/4572956.html