1. 程式人生 > >設計模式--責任鏈

設計模式--責任鏈

責任鏈,處理節節相扣。

比如十個類,有序的呼叫其他九個類,環環相扣。

怎麼設計比較簡潔,要是還想簡單設計,連抽象類都搞出來

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();
    }
}