package com.jmj.pattern.responsibility;

/**
 * 请假条类
 */
public class LeaveRequest {
    //姓名
    private String name;
    //请假天数
    private int num;
   //请假内容
    private String content;

    public LeaveRequest(String name, int num, String content) {
        this.name = name;
        this.num = num;
        this.content = content;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNum() {
        return num;
    }

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

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}
package com.jmj.pattern.responsibility;

public abstract class Handler {
    protected final static int NUM_ONE=1;
    protected final static int NUM_THREE=3;
    protected final static int NUM_SEVEN=7;

    //该领导处理请求天数区间
    private int numStart;
    private int numEnd;


    private Handler nextHandler;

    public Handler(int numStart) {
        this.numStart = numStart;
    }

    public Handler(int numStart, int numEnd) {
        this.numStart = numStart;
        this.numEnd = numEnd;
    }

    public void setNextHandler(Handler nextHandler) {
        this.nextHandler = nextHandler;
    }

    //各级领导处理请假条的方法
    protected abstract void handleLeave(LeaveRequest leave);

    public final void submit(LeaveRequest leave){

        if (this.nextHandler!=null&&leave.getNum()>this.numEnd){
            this.nextHandler.submit(leave);
        }else {
            this.handleLeave(leave);
            System.out.println("流程结束 !");
        }


    }

}
package com.jmj.pattern.responsibility;

public class GeneralManager extends Handler{
    public GeneralManager(){
        super(Handler.NUM_THREE,Handler.NUM_SEVEN);
    }

    @Override
    protected void handleLeave(LeaveRequest leave) {
        System.out.println(leave.getName()+"请假"+leave.getNum()+"天,"+leave.getContent()+"。");
        System.out.println("总经理审批:同意");
    }
}
package com.jmj.pattern.responsibility;

public class Manager extends Handler{
    public Manager(){
        super(Handler.NUM_ONE,Handler.NUM_THREE);
    }

    @Override
    protected void handleLeave(LeaveRequest leave) {
        System.out.println(leave.getName()+"请假"+leave.getNum()+"天,"+leave.getContent()+"。");
        System.out.println("部门经理审批:同意");
    }
}
package com.jmj.pattern.responsibility;

public class GroupLeader extends Handler{
    public GroupLeader(){
        super(0,Handler.NUM_ONE);
    }

    @Override
    protected void handleLeave(LeaveRequest leave) {
        System.out.println(leave.getName()+"请假"+leave.getNum()+"天,"+leave.getContent()+"。");
        System.out.println("小组审批:同意");
    }
}
package com.jmj.pattern.responsibility;

public class Client {
    public static void main(String[] args) {
        LeaveRequest leaveRequest = new LeaveRequest("张三", 5, "身体不适");
        GroupLeader groupLeader = new GroupLeader();
        Manager manager = new Manager();
        GeneralManager generalManager = new GeneralManager();
        groupLeader.setNextHandler(manager);

        manager.setNextHandler(generalManager);

        groupLeader.submit(leaveRequest);
    }
}

原文地址:https://blog.csdn.net/qq_53374893/article/details/134760594

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_30816.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注