请假案例的代码实现:
/**
* 封装请假的基本信息
* @author 万河归海
*
*/
public class LeaveRequest {
private String empName;
private int LeavesDays;
private String reason;
public LeaveRequest(String empName, int leavesDays, String reason) {
super();
this.empName = empName;
LeavesDays = leavesDays;
this.reason = reason;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getLeavesDays() {
return LeavesDays;
}
public void setLeavesDays(int leavesDays) {
LeavesDays = leavesDays;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
}
/**
* 主任
* @author 万河归海
*
*/
public class Director extends Leader{
public Director(String name) {
super(name);
}
@Override
public void handleRequest(LeaveRequest request) {
if(request.getLeavesDays()<3){
System.out.println("员工:"+request.getEmpName()+",请假"+request.getLeavesDays()+",理由是"+request.getReason());
System.out.println("主任:"+this.name+"同意");
}else{
if(this.nextLeader!=null){
System.out.println("主任:"+this.name+"同意");
this.nextLeader.handleRequest(request);
}
}
}
}
public class Manager extends Leader{
public Manager(String name) {
super(name);
}
@Override
public void handleRequest(LeaveRequest request) {
if(request.getLeavesDays()<10){
System.out.println("员工:"+request.getEmpName()+",请假"+request.getLeavesDays()+",理由是"+request.getReason());
System.out.println("经理:"+this.name+"同意");
}else{
if(this.nextLeader!=null){
System.out.println("经理:"+this.name+"同意");
this.nextLeader.handleRequest(request);
}
}
}
}
/**
* 总经理
* @author 万河归海
*
*/
public class GeneralManager extends Leader{
public GeneralManager(String name) {
super(name);
}
@Override
public void handleRequest(LeaveRequest request) {
if(request.getLeavesDays()<30){
System.out.println("员工:"+request.getEmpName()+",请假"+request.getLeavesDays()+",理由是"+request.getReason());
System.out.println("总经理:"+this.name+"同意");
}else{
System.out.println("拒绝");
}
}
}
public class Test {
public static void main(String[] args) {
Leader d = new Director("张三");
Leader m = new Manager("李四");
Leader gm = new GeneralManager("王五");
//设置责任链后继对象
d.setNextLeader(m);
m.setNextLeader(gm);
LeaveRequest request = new LeaveRequest("tom", 29, "旅行");
d.handleRequest(request);
}
}