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);
}
}