大话设计模式-职责链模式的实践-请求加薪

UML

申请

/** * 申请 */
public class Request {
    private String type;
    private String content;
    private int num;

    public Request() {
    }

    public Request(String type, String content, int num) {
        this.type = type;
        this.content = content;
        this.num = num;
    }

    @Override
    public String toString() {
        return "Request{" +
                "type='" + type + '\'' +
                ", content='" + content + '\'' +
                ", num=" + num +
                '}';
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}

经理

/** * 经理 */
public class CommonManager extends Manager {
    public CommonManager(String name) {
        super(name);
    }

    /** * 经理的权限是准许两天内的假期 * * @param request */
    @Override
    public void requestApplications(Request request) {
        if ("请假".equals(request.getType()) && request.getNum() <= 2) {
            System.out.println("经理 " + name + " 准许 " + request.toString());
        } else if (superior != null) {
            superior.requestApplications(request);
        }
    }
}

总监

/** * 总监 */
public class Majordomo extends Manager {
    public Majordomo(String name) {
        super(name);
    }

    /** * 总监的权限是准许五天内的假期 * * @param request */
    @Override
    public void requestApplications(Request request) {
        if ("请假".equals(request.getType()) && request.getNum() <= 5) {
            System.out.println("总监 " + name + " 准许 " + request.toString());
        } else if (superior != null) {
            superior.requestApplications(request);
        }
    }
}

总经理

/** * 总经理 */
public class GeneraManager extends Manager {
    public GeneraManager(String name) {
        super(name);
    }

    /** * 总经理的权限是准许任意天内的假期 * * @param request */
    @Override
    public void requestApplications(Request request) {
        if ("请假".equals(request.getType())) {
            System.out.println("总经理 " + name + " 准许 " + request.toString());
        } else if ("加薪".equals(request.getType()) && request.getNum() <= 500) {
            System.out.println("总经理 " + name + " 准许 " + request.toString());
        } else if ("加薪".equals(request.getType()) && request.getNum() > 500) {
            System.out.println("总经理 " + name + " 这个请求我再考虑考虑 " + request.toString());
        }
    }
}

测试类

/** * 测试类 */
public class Main {
    public static void main(String[] args) {
        CommonManager commonManager = new CommonManager("王经理");
        Majordomo majordomo = new Majordomo("李总监");
        GeneraManager generaManager = new GeneraManager("张总经理");

        commonManager.setSuperior(majordomo);
        majordomo.setSuperior(generaManager);

        Request request1 = new Request("请假", "我感冒了要请假!", 2);
        commonManager.requestApplications(request1);

        Request request2 = new Request("请假", "我感冒了要请假!", 3);
        commonManager.requestApplications(request2);

        Request request3 = new Request("加薪", "我要加薪!!!", 500);
        commonManager.requestApplications(request3);

        Request request4 = new Request("加薪", "我要加薪!!!", 1000);
        commonManager.requestApplications(request4);

    }
}

运行结果

全部评论

相关推荐

10-21 23:48
蚌埠坦克学院
csgq:可能没hc了 昨天一面完秒挂
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务