责任链模式

2019-05-07  本文已影响0人  Davisxy

定义

场景

小栗子

package com.principle.chainofresp;

/**
 * 请假条的基本信息
 * 
 * @author xy
 *
 */
public class LeaveRequest {

    private String empName;
    private int leaveDays;
    private String reason;

    public LeaveRequest(String empName, int leaveDays, String reason) {
        super();
        this.empName = empName;
        this.leaveDays = leaveDays;
        this.reason = reason;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public int getLeaveDays() {
        return leaveDays;
    }

    public void setLeaveDays(int leaveDays) {
        this.leaveDays = leaveDays;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }
}
package com.principle.chainofresp;

/**
 * 主任
 * @author xy
 *
 */
public class Director extends Leader{

    public Director(String name) {
        super(name);
    }

    @Override
    public void handleRequest(LeaveRequest request) {
        if (request.getLeaveDays()<3) {
            System.out.println("员工:"+request.getEmpName()+"请假,天数:"+request.getLeaveDays()+",原因:"+request.getReason());
            System.out.println("主任:"+this.name+",审核通过");
        }else {
            if (this.nextLeader!=null) {
                this.nextLeader.handleRequest(request);
            }
        }
    }

}
package com.principle.chainofresp;

/**
 * 主任
 * @author xy
 *
 */
public class Manager extends Leader{

    public Manager(String name) {
        super(name);
    }

    @Override
    public void handleRequest(LeaveRequest request) {
        if (request.getLeaveDays()<10) {
            System.out.println("员工:"+request.getEmpName()+"请假,天数:"+request.getLeaveDays()+",原因:"+request.getReason());
            System.out.println("经理:"+this.name+",审核通过");
        }else {
            if (this.nextLeader!=null) {
                this.nextLeader.handleRequest(request);
            }
        }
    }

}
package com.principle.chainofresp;

/**
 * 副总经理
 * @author xy
 *
 */
public class ViceGeneraManager extends Leader{

    public ViceGeneraManager(String name) {
        super(name);
    }

    @Override
    public void handleRequest(LeaveRequest request) {
        if (request.getLeaveDays()<20) {
            System.out.println("员工:"+request.getEmpName()+"请假,天数:"+request.getLeaveDays()+",原因:"+request.getReason());
            System.out.println("副总经理:"+this.name+",审核通过");
        }else {
            if (this.nextLeader!=null) {
                this.nextLeader.handleRequest(request);
            }
        }
    }

}
package com.principle.chainofresp;

/**
 * 主任
 * @author xy
 *
 */
public class GeneraManager extends Leader{

    public GeneraManager(String name) {
        super(name);
    }

    @Override
    public void handleRequest(LeaveRequest request) {
        if (request.getLeaveDays()<30) {
            System.out.println("员工:"+request.getEmpName()+"请假,天数:"+request.getLeaveDays()+",原因:"+request.getReason());
            System.out.println("总经理:"+this.name+",审核通过");
        }else {
            System.out.println("想辞职不干了???");
        }
    }

}
package com.principle.chainofresp;

public class Client {
    public static void main(String[] args) {
        Leader a=new Director("张三");
        Leader b=new Manager("李四");
        Leader c=new ViceGeneraManager("李小四");
        Leader d=new GeneraManager("王五");
        
        
        //组织责任链对象的关系
        a.setNextLeader(b);
        b.setNextLeader(c);
        c.setNextLeader(d);
        
        //开始请假操作
        LeaveRequest req1=new LeaveRequest("Tom", 23, "回家探亲");
        a.handleRequest(req1);
    }
}
控制台打印:
员工:Tom请假,天数:23,原因:回家探亲
总经理:王五,审核通过

类图

责任链模式.png
上一篇下一篇

猜你喜欢

热点阅读