23种模式的归类:有3种
行为型模式有11种模式
责任链模式chain of responsibility
类图
//--------------------封装请假的基本信息-------------
package java_243_GOF23设计模式_责任链模式_公文审批_供应链系统的采购审批_异常链_过滤器和拦截器调用过程_练习;
/**
-
封装请假的基本信息
/
public class LeaveRequest {//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;
}
//set与get方法
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 java_243_GOF23设计模式_责任链模式_公文审批_供应链系统的采购审批_异常链_过滤器和拦截器调用过程_练习;
/* -
抽象类
/
public abstract class Leader {//abstract抽象的
//属性
protected String name;//需要供给子类使用;使用不能使用private
protected Leader nextLeader;//责任链上的后继对象;即上级领导
public Leader(String name) {
super();
this.name = name;
}
//设定责任链上的后继对象
public void setNextLeader(Leader nextLeader) {
this.nextLeader = nextLeader;
}
//定义抽象方法; 这是处理业务核心方法
public abstract void handleRequest(LeaveRequest request);
}
//------------------主任类-------------------------
package java_243_GOF23设计模式_责任链模式_公文审批_供应链系统的采购审批_异常链_过滤器和拦截器调用过程_练习;
/* -
主任类
*/
public class Director extends Leader{//Director主任//重写
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 java_243_GOF23设计模式_责任链模式_公文审批_供应链系统的采购审批_异常链_过滤器和拦截器调用过程_练习;
/** -
经理类
*/
public class Manager extends Leader{//Director主任//重写
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 java_243_GOF23设计模式_责任链模式_公文审批_供应链系统的采购审批_异常链_过滤器和拦截器调用过程_练习;
/** -
总经理类
*/
public class GeneralManager extends Leader{//Director主任//重写
public GeneralManager(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(“莫非:”+request.getEmpName()+“想辞职,居然请假:”+request.getLeaveDays()+",天!");
}
}
}
//----------------------测试-------------------------------
package java_243_GOF23设计模式_责任链模式_公文审批_供应链系统的采购审批_异常链_过滤器和拦截器调用过程_练习;
public class Client {
public static void main(String[] args) {
Leader a = new Director(“张三”); //主任
Leader b = new Manager(“李四”);//经理
Leader c = new GeneralManager(“王伟”);
//组织责任链对象之间的关系
a.setNextLeader(b);
b.setNextLeader(c);
//请假操作
LeaveRequest req = new LeaveRequest("宋斌", 2, "事假");
a.handleRequest(req);//handleRequest;前面定义的核心部分
System.out.println("=================");
LeaveRequest req1 = new LeaveRequest("李静", 15, "探亲");
a.handleRequest(req1);//handleRequest;前面定义的核心部分
System.out.println("=================");
LeaveRequest req2 = new LeaveRequest("王磊", 30, "探亲");
a.handleRequest(req2);//handleRequest;前面定义的核心部分
}
}
//-------------------结果-------------------------
//--------------------添加角色-------------------------
package java_243_GOF23设计模式_责任链模式_公文审批_供应链系统的采购审批_异常链_过滤器和拦截器调用过程_练习;
/**
-
副总经理类
*/
public class ViceGeneralManager extends Leader{//Director主任//重写
public ViceGeneralManager(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 java_243_GOF23设计模式_责任链模式_公文审批_供应链系统的采购审批_异常链_过滤器和拦截器调用过程_练习;
public class Client {
public static void main(String[] args) {
Leader a = new Director(“张三”); //主任
Leader b = new Manager(“李四”);//经理
Leader c = new GeneralManager(“王伟”);
Leader b2 = new ViceGeneralManager(“赵六”);
//组织责任链对象之间的关系
a.setNextLeader(b);
b.setNextLeader(b2);
b2.setNextLeader(c);
//请假操作
LeaveRequest req = new LeaveRequest("宋斌", 2, "事假");
a.handleRequest(req);//handleRequest;前面定义的核心部分
System.out.println("=================");
LeaveRequest req1 = new LeaveRequest("李静", 15, "探亲");
a.handleRequest(req1);//handleRequest;前面定义的核心部分
System.out.println("=================");
LeaveRequest req2 = new LeaveRequest("王磊", 30, "探亲");
a.handleRequest(req2);//handleRequest;前面定义的核心部分
}
}
//--------------结果----------------------
其他方法实现