信息主体
package com.design.pattern.factory.chainOfResp;
import java.util.Date;
/**
* 封装请假的基本信息
* @author Administrator
*
*/
public class LeaveRequest {
/**
* 请假人
*/
private String empName;
/**
* 请假天数
*/
private int leaveDays;
/**
* 请假原因
*/
private String reason;
/**
* 请假开始时间
*/
private Date StartDate;
public LeaveRequest(String empName, int leaveDays, String reason, Date startDate) {
super();
this.empName = empName;
this.leaveDays = leaveDays;
this.reason = reason;
StartDate = startDate;
}
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;
}
public Date getStartDate() {
return StartDate;
}
public void setStartDate(Date startDate) {
StartDate = startDate;
}
}
信息流程接口
package com.design.pattern.factory.chainOfResp;
/**
* 抽象类
* @author Administrator
*
*/
public abstract class Leader {
protected String name;
protected Leader nextLeader; //责任链上的后继对象
protected Leader preLeader; //责任链上的后继对象
protected int days; //批准的天数上限
public Leader() {
}
public Leader(String name, int days) {
super();
this.name = name;
this.days = days;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDays() {
return days;
}
public void setDays(int days) {
this.days = days;
}
public Leader getNextLeader() {
return nextLeader;
}
//设定责任链上的后继对象
public void setNextLeader(Leader nextLeader) {
this.nextLeader = nextLeader;
}
public Leader getPreLeader() {
return preLeader;
}
//设定责任链上的前驱对象
public void setPreLeader(Leader preLeader) {
this.preLeader = preLeader;
}
/**
* 处理请求的核心的业务方法,正向请假,从下级到上级
* @param request
*/
public abstract void handleRequest(LeaveRequest request,Leader leader);
/**
* 从上级到下级审批
* @param request
* @param leader
*/
public abstract void handleResponse(LeaveRequest request,Leader leader);
}
经理
package com.design.pattern.factory.chainOfResp;
public class Manger extends Leader{
public Manger(String name, int days) {
super(name, days);
// TODO Auto-generated constructor stub
}
@Override
public void handleRequest(LeaveRequest request,Leader leader) {
if(request.getLeaveDays()<leader.getDays()){
System.out.println("员工:"+request.getEmpName()+"请假,天数:"+request.getLeaveDays()+",理由:"+request.getReason());
System.out.println(this.name+",审批通过!");
if(leader.preLeader!=null){
leader=leader.preLeader;
this.preLeader.handleResponse(request,leader);
}
}else{
if(this.nextLeader!=null){
System.out.println("员工:"+request.getEmpName()+"请假:"+request.getLeaveDays()+"天,超出"+leader.getName()+"批假权限,请"+leader.getNextLeader().getName()+"审批");
leader=leader.nextLeader;
this.nextLeader.handleRequest(request,leader);
}else{
System.out.println(this.name+",请假"+request.getLeaveDays()+"太久"+leader.getName()+"审批不予通过!");
}
}
}
public void handleResponse(LeaveRequest request,Leader leader){
System.out.println(leader.getName()+"批准!");
if(leader.preLeader!=null){
leader=leader.preLeader;
this.preLeader.handleResponse(request,leader);
}
}
}
副总
package com.design.pattern.factory.chainOfResp;
/**
* 副总经理
* @author Administrator
*
*/
public class ViceGeneralManager extends Leader {
public ViceGeneralManager() {
super();
// TODO Auto-generated constructor stub
}
public ViceGeneralManager(String name, int days) {
super(name, days);
// TODO Auto-generated constructor stub
}
@Override
public void handleRequest(LeaveRequest request,Leader leader) {
if(request.getLeaveDays()<leader.getDays()){
System.out.println("员工:"+request.getEmpName()+"请假,天数:"+request.getLeaveDays()+",理由:"+request.getReason());
System.out.println(this.name+",审批通过!");
if(leader.preLeader!=null){
leader=leader.preLeader;
this.preLeader.handleResponse(request,leader);
}
}else{
if(this.nextLeader!=null){
System.out.println("员工:"+request.getEmpName()+"请假:"+request.getLeaveDays()+"天,超出"+leader.getName()+"批假权限,请"+leader.getNextLeader().getName()+"审批");
leader=leader.nextLeader;
this.nextLeader.handleRequest(request,leader);
}else{
System.out.println(this.name+",请假"+request.getLeaveDays()+"太久"+"审批不予通过!");
}
}
}
public void handleResponse(LeaveRequest request,Leader leader){
System.out.println(leader.getName()+"批准!");
if(leader.preLeader!=null){
leader=leader.preLeader;
this.preLeader.handleResponse(request,leader);
}
}
}
总经理:
package com.design.pattern.factory.chainOfResp;
public class GeneralManger extends Leader{
public GeneralManger() {
super();
}
public GeneralManger(String name, int days) {
super(name, days);
}
@Override
public void handleRequest(LeaveRequest request,Leader leader) {
if(request.getLeaveDays()<leader.getDays()){
System.out.println("员工:"+request.getEmpName()+"请假:"+request.getLeaveDays()+"天,理由:"+request.getReason());
System.out.println(this.name+",审批通过!");
if(leader.preLeader!=null){
leader=leader.preLeader;
this.preLeader.handleResponse(request,leader);
}
}else{
if(this.nextLeader!=null){
System.out.println("员工:"+request.getEmpName()+"请假:"+request.getLeaveDays()+"天,超出"+leader.getName()+"批假权限,请"+leader.getNextLeader().getName()+"审批");
leader=leader.nextLeader;
this.nextLeader.handleRequest(request,leader);
}else{
System.out.println(this.name+",请假"+request.getLeaveDays()+"太久"+"审批不予通过!");
}
}
}
public void handleResponse(LeaveRequest request,Leader leader){
System.out.println(leader.getName()+"批准!");
if(leader.preLeader!=null){
leader=leader.preLeader;
this.preLeader.handleResponse(request,leader);
}
}
}
测试:
package com.design.pattern.factory.chainOfResp;
import java.util.Date;
public class Client {
public static void main(String[] args) {
Leader a = new Manger("王经理",10);
Leader b = new ViceGeneralManager("李副总",20);
Leader c = new GeneralManger("杨总",30);
//组织责任链对象的关系
a.setNextLeader(b);
b.setNextLeader(c);
b.setPreLeader(a);
c.setPreLeader(b);
//开始请假操作
LeaveRequest req = new LeaveRequest("jack", 15, "回家相亲!",new Date());
a.handleRequest(req,a);
}
}
结果:
员工:jack请假:22天,超出王经理批假权限,请李副总审批
员工:jack请假:22天,超出李副总批假权限,请杨总审批
员工:jack请假:22天,理由:回家相亲!
杨总,审批通过!
李副总批准!
王经理批准!