《软件设计》职责链模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、理解职责链模式的动机,掌握该模式的结构;

2、能够利用职责链模式解决实际问题。

 


[实验任务一]:财务审批
某物资管理系统中物资采购需要分级审批,主任可以审批1万元及以下的采购单,部门经理可以审批5万元及以下的采购单,副总经理可以审批10万元及以下的采购单,总经理可以审批20万元及以下的采购单,20万元以上的采购单需要开职工大会确定。现用职责链模式设计该系统。
实验要求:

1. 提交类图;

2. 提交源代码;

3. 注意编程规范。

 

1、类图

2、源代码

DepartmentManager.java

class DepartmentManager extends Leader{

    public DepartmentManager(String name) {

        super(name);

    }

 

    protected void handleRequest(ShoppingRequest request) {

        if (request.getBillMoney()<50000.00){

            System.out.println(name + "部门经理处理物资采购" + request.getShoppingReason() + "采购资金为" + request.getBillMoney() + "元");

        } else {

            if (this.successor != null){

                this.successor.handleRequest(request);

            }

        }

    }

}

 

Director.java

class Director extends Leader{
    public Director(String name) {
        super(name);
    }

    public void handleRequest(ShoppingRequest request){
        if (request.getBillMoney()<10000.00){
            System.out.println(name + "主任处理物资采购" + request.getShoppingReason() + "采购资金为" + request.getBillMoney() + "元");
        } else {
            if (this.successor != null){
                this.successor.handleRequest(request);
            }
        }
    }
}

GeneralManager.java

class GeneralManager extends Leader{
    public GeneralManager(String name) {
        super(name);
    }

    @Override
    protected void handleRequest(ShoppingRequest request) {
        if (request.getBillMoney()<200000.00){
            System.out.println(name + "总经理处理物资采购" + request.getShoppingReason() + "采购资金为" + request.getBillMoney() + "元");
        } else {
            System.out.println("董事会将开会处理物资采购" + request.getShoppingReason() + "采购资金为" + request.getBillMoney() + "元");
        }
    }
}

Leader.java

package org.example;

public abstract class Leader {
    protected String name;

    //上级领导
    protected Leader successor;

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

    public void setSuccessor(Leader successor) {
        this.successor = successor;
    }

    protected abstract void handleRequest(ShoppingRequest request);
}

ShoppingRequest.java

package org.example;

public class ShoppingRequest {
    private String shoppingReason;
    private double billMoney;

    public ShoppingRequest(String shoppingReason,double billMoney){
        this.shoppingReason = shoppingReason;
        this.billMoney = billMoney;
    }

    public String getShoppingReason() {
        return shoppingReason;
    }

    public void setShoppingReason(String shoppingReason) {
        this.shoppingReason = shoppingReason;
    }

    public double getBillMoney() {
        return billMoney;
    }

    public void setBillMoney(double billMoney) {
        this.billMoney = billMoney;
    }
}

ViceManager.java

package org.example;

class ViceManager extends Leader{
    public ViceManager(String name) {
        super(name);
    }

    @Override
    protected void handleRequest(ShoppingRequest request) {
        if (request.getBillMoney()<100000.00){
            System.out.println(name + "副经理处理物资采购" + request.getShoppingReason() + "采购资金为" + request.getBillMoney() + "元");
        } else {
            if (this.successor != null){
                this.successor.handleRequest(request);
            }
        }
    }
}

Main.java

package org.example;

public class Main {
    public static void main(String[] args) {
        Leader objDirector,objDepartmentManager,objViceManager,objGeneralManager;
        objDirector = new Director("张主任");
        objDepartmentManager = new DepartmentManager("刘部门经理");
        objViceManager = new ViceManager("陈副经理");
        objGeneralManager = new GeneralManager("陈经理");

        //设置上级
        objDirector.setSuccessor(objDepartmentManager);
        objDepartmentManager.setSuccessor(objViceManager);
        objViceManager.setSuccessor(objGeneralManager);

        ShoppingRequest shoppingRequest1 = new ShoppingRequest("买打印纸",1000);
        ShoppingRequest shoppingRequest2 = new ShoppingRequest("购置电脑",20000);
        ShoppingRequest shoppingRequest3 = new ShoppingRequest("购置空调",80000);
        ShoppingRequest shoppingRequest4 = new ShoppingRequest("办公室装修",160000);
        ShoppingRequest shoppingRequest5 = new ShoppingRequest("收购XX公司",1000000);

        objDirector.handleRequest(shoppingRequest1);
        System.out.println("****************************************");
        objDirector.handleRequest(shoppingRequest2);
        System.out.println("****************************************");
        objDirector.handleRequest(shoppingRequest3);
        System.out.println("****************************************");
        objDirector.handleRequest(shoppingRequest4);
        System.out.println("****************************************");
        objDirector.handleRequest(shoppingRequest5);
        System.out.println("****************************************");
    }
}

 

 

posted @ 2024-11-21 00:39  new菜鸟  阅读(10)  评论(0编辑  收藏  举报