链式处理

public abstract class AbstractHandler {

    private static AbstractHandler handler = new Handler1();

    static {
        handler.setNextHandler(new Handler2())
                .setNextHandler(new Handler3());
    }

    private AbstractHandler nextHandler;

    protected void handle(){
        if(this.nextHandler != null){
            this.nextHandler.handle();
        }
    }

/**
*
* 流程链前置日志
*/
protected String getPreLog() {
return "handle";
}
public AbstractHandler setNextHandler(AbstractHandler nextHandler) { this.nextHandler = nextHandler; return this.nextHandler; } public static AbstractHandler getHandler(){ return handler; } }

 

public class Handler1 extends AbstractHandler{
    @Override
    protected void handle() {
        System.out.println("handle-1");
        super.handle();
    }
}
public class Test {
    public static void main(String[] args) {
        AbstractHandler.getHandler().handle();
    }
}

 

posted on 2019-08-10 16:18  CoDeiSlifE  阅读(205)  评论(0编辑  收藏  举报