美文网首页
职责链模式

职责链模式

作者: 提拉米苏_cad2 | 来源:发表于2017-09-06 20:08 被阅读0次

    1、抽象基类

    public abstract classHandler {

    protected Handler handlerNext;

    public abstractString handlerRequest(String user,doublefee);

    publicHandler getHandlerNext() {

    return HandlerNext;

    }

    public void setHandlerNext(Handler handlerNext) {

    HandlerNext= handlerNext;

    }

    }

    2、具体实现类

    1)public classProjectManagerextendsHandler {

    @Override

    publicString handlerRequest(String user,doublefee) {

    String response =newString("project");

    return this.getHandlerNext().handlerRequest("user", fee);

    }

    }

    2)

    public classDeptManagerextendsHandler {

    @Override

    publicString handlerRequest(String user,doublefee) {

    String response =newString("Dept manager");

    return response;

    }

    }

    3、测试+组装职责链

    public classtest {

    public static voidmain(String[] args) {

    //组装职责链

    ProjectManager projectManager =newProjectManager();

    DeptManager deptManager =newDeptManager();

    projectManager.setHandlerNext(deptManager);

    String response = projectManager.handlerRequest("zhangsan",34);

    }

    }

    相关文章

      网友评论

          本文标题:职责链模式

          本文链接:https://www.haomeiwen.com/subject/bpivjxtx.html