*模拟场景:
- 员工出差,会到公司需要报销(报销金额未知)
- 组长只能报销1000块钱以内费用,超出1000,组长需要汇报给主管。
- 主管只能报销3000块钱以内费用,超出3000,主管需要汇报给经理。
- 经理只能报销5000块钱以内费用,超出5000,主管需要汇报给董事长。
- 董事长只能报销10000块钱以内费用,超出10000,公司拒绝报销了。、
- 抽象的领导者类
Leader 类
public abstract class Leader {
//子类需要实现的两个方法(能力,处理事务)
public abstract int limit();
public abstract void handle(int money);
//上级领导者
protected Leader nextLeader;
public final void handleRequest(int money){
if (money<=limit()){
handle(money);
}else {
if (null!=nextLeader){
nextLeader.handleRequest(money);
}else{
System.out.println("报销超过了最高金额,无法报销");
}
}
}
}
具体的领导类
- GroupLeader 类
public class GroupLeader extends Leader {
//比如组长只能处理1000块钱的报销
@Override
public int limit() {
return 1000;
}
@Override
public void handle(int money) {
System.out.println("组长批复报销了"+money+"元");
}
}
- Director 类
public class Director extends Leader {
//比如主管只能处理3000块钱的报销
@Override
public int limit() {
return 3000;
}
@Override
public void handle(int money) {
System.out.println("主管批复报销了"+money+"元");
}
}
- Manager 类
public class Manager extends Leader {
//比如经理只能处理5000块钱的报销
@Override
public int limit() {
return 5000;
}
@Override
public void handle(int money) {
System.out.println("经理批复报销了"+money+"元");
}
}
- Boss 类
public class Boss extends Leader {
//比如董事长只能处理10000块钱的报销
@Override
public int limit() {
return 10000;
}
@Override
public void handle(int money) {
System.out.println("董事长批复报销了"+money+"元");
}
}
- Employee 类
public class Employee {
public static void main(String args[]){
GroupLeader groupLeader=new GroupLeader();
Director director=new Director();
Manager manager=new Manager();
Boss boss=new Boss();
groupLeader.nextLeader=director;
director.nextLeader=manager;
manager.nextLeader=boss;
groupLeader.handleRequest(6000);
}
}
- 运行结果
董事长批复报销了6000元