设计模式--责任链

责任链,处理节节相扣。

比如十个类,有序的调用其他九个类,环环相扣。

怎么设计比较简洁,要是还想简单设计,连抽象类都搞出来

public class test {
    public abstract class Handler{
        public Handler handler;
        public void setHandler(Handler handler) {
            this.handler = handler;
        }
        public void doSomeThing() {        
            System.out.println(this.toString());
            if(this.handler == null) {
                return;
            }
            handler.doSomeThing();
        }
    }
    public class DoSomeThing extends Handler{
        
    }
    public static void main(String[] args) {
        test.DoSomeThing t1 = new test().new DoSomeThing();
        test.DoSomeThing t2 = new test().new DoSomeThing();
        t1.setHandler(t2);
        t1.doSomeThing();
    }
}

猜你喜欢

转载自blog.csdn.net/huangddy/article/details/84262821