Java责任链模式

公司里面系统中,采购审批子系统的设计:

  1. 采购金额小于5万,主任审批
  2. 采购金额大于等于5万,小于10万,经理审批
  3. 采购金额大于等于10万,小于20万,副总经理审批
  4. 采购金额大于等于20万,总经理审批

封装采购的基本信息PurchasingInformation.java

/**
 * 封装采购的基本信息
 */
public class PurchasingInformation {
    //员工姓名
    private String empName;
    //采购金额
    private double money;
    //采购理由
    private String reason;

    public PurchasingInformation(String empName, double money, String reason) {
        this.empName = empName;
        this.money = money;
        this.reason = reason;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }
}

抽象类Leader.java

/**
 * 抽象类
 */
public abstract class Leader {
    protected String name;
    //责任链上的后继对象
    protected Leader nextLeader;

    public Leader(String name) {
        this.name = name;
    }

    public void setNextLeader(Leader nextLeader) {
        this.nextLeader = nextLeader;
    }

    /**
     * 处理请求的核心的业务方法
     *
     * @param information
     */
    public abstract void handleRequest(PurchasingInformation information);

}

主任类Director.java

/**
 * 主任
 */
public class Director extends Leader{

    public Director(String name) {
        super(name);
    }

    @Override
    public void handleRequest(PurchasingInformation information) {
        if (information.getMoney() < 50000.00) {
            System.out.printf("员工:%s,购买金额:%.2f元,购买理由:%s%n", information.getEmpName(), information.getMoney(), information.getReason());
            System.out.println("主任:" + this.name + ",审批通过");
        } else {
            if (this.nextLeader != null) {
                this.nextLeader.handleRequest(information);
            }
        }
    }
}

经理类Manager.java

/**
 * 经理
 */
public class Manager extends Leader{
    public Manager(String name) {
        super(name);
    }

    @Override
    public void handleRequest(PurchasingInformation information) {
        if (information.getMoney() < 100000.00) {
            System.out.printf("员工:%s,购买金额:%.2f元,购买理由:%s%n", information.getEmpName(), information.getMoney(), information.getReason());
            System.out.println("经理:"+this.name+"审批通过");
        }else {
            if (this.nextLeader != null) {
                this.nextLeader.handleRequest(information);
            }
        }
    }
}

副总经理类ViceGeneraManger.java

/**
 * 副总经理
 */
public class ViceGeneraManger extends Leader {
    public ViceGeneraManger(String name) {
        super(name);
    }

    @Override
    public void handleRequest(PurchasingInformation information) {
        if (information.getMoney() < 200000.00) {
            System.out.printf("员工:%s,购买金额:%.2f元,购买理由:%s%n", information.getEmpName(), information.getMoney(), information.getReason());
            System.out.println("副总经理:" + this.name + "审批通过");
        } else {
            if (this.nextLeader != null) {
                this.nextLeader.handleRequest(information);
            }
        }
    }
}

总经理类GeneraManger.java

/**
 * 总经理
 */
public class GeneraManger extends Leader{
    public GeneraManger(String name) {
        super(name);
    }

    @Override
    public void handleRequest(PurchasingInformation information) {
        if (information.getMoney() >= 200000.00) {
            System.out.printf("员工:%s,购买金额:%.2f元,购买理由:%s%n", information.getEmpName(), information.getMoney(), information.getReason());
            System.out.println("总经理:"+this.name+"审批通过");
        }
    }
}

主程序Client.java

public class Client {
    public static void main(String[] args) {
        Leader director = new Director("张三");
        Leader manger = new Manager("李四");
        Leader viceGeneraManger = new ViceGeneraManger("王五");
        Leader generaManger = new GeneraManger("张三丰");

        //组织关系
        director.setNextLeader(manger);
        manger.setNextLeader(viceGeneraManger);
        viceGeneraManger.setNextLeader(generaManger);

        //开始审批操作
        PurchasingInformation information1 = new PurchasingInformation("小明", 20000.00, "需要新设备");
        director.handleRequest(information1);
        //分割
        System.out.println("----------");
        PurchasingInformation information2 = new PurchasingInformation("小华", 80000.00, "需要新设备");
        director.handleRequest(information2);
        System.out.println("----------");
        PurchasingInformation information3 = new PurchasingInformation("小峰", 150000.00, "需要新设备");
        director.handleRequest(information3);
        System.out.println("----------");
        PurchasingInformation information4 = new PurchasingInformation("小花", 300000.00, "需要新设备");
        director.handleRequest(information4);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值