11.14

实验15:职责链模式

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

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

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

 

[实验任务一]:财务审批

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

实验要求:

1、类图

 

 

2、源代码

①、Leader.java

package org.example.shiyanshiwu;

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;
    }

    public abstract void handleRequest(PurchaseRequest request);
}

 

②、Director.java

package org.example.shiyanshiwu;

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

    @Override
    public void handleRequest(PurchaseRequest request) {
        if (request.getMoney() <= 10000) {
            System.out.println("主任" + name + "审批采购单,采购单号为:" + request.getId() + ",金额为:" + request.getMoney() + "");
        } else {
            if (this.successor != null) {
                this.successor.handleRequest(request);
            }
        }
    }
}

 

③、DepartmentManager.java

package org.example.shiyanshiwu;

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

    @Override
    public void handleRequest(PurchaseRequest request) {
        if (request.getMoney() <= 50000) {
            System.out.println("部门经理" + name + "审批采购单,采购单号为:" + request.getId() + ",金额为:" + request.getMoney() + "");
        } else {
            if (this.successor != null) {
                this.successor.handleRequest(request);
            }
        }
    }
}

 

④、Manager.java

package org.example.shiyanshiwu;

public class Manager extends Leader {


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

    @Override
    public void handleRequest(PurchaseRequest request) {
        if (request.getMoney() <= 100000) {
            System.out.println("副总经理" + name + "审批采购单,采购单单号为:" + request.getId() + ",金额为:" + request.getMoney() + "");
        } else {
            if (this.successor != null) {
                this.successor.handleRequest(request);
            }
        }
    }
}

 

⑤、GeneralManager.java

package org.example.shiyanshiwu;

public class GeneralManager extends Leader {

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

    @Override
    public void handleRequest(PurchaseRequest request) {
        if (request.getMoney() <= 200000) {
            System.out.println("总经理" + name + "审批采购单,采购单单号为:" + request.getId() + ",金额为:" + request.getMoney() + "");
        } else {
            System.out.println("采购单单号为:" + request.getId() + ",金额为:" + request.getMoney() + "元,金额太大,需开职工大会确定是否审批通过");
        }
    }
}

 

⑥、PurchaseRequest.java

package org.example.shiyanshiwu;

public class PurchaseRequest {
    private int money;
    private int id;

    public PurchaseRequest() {
    }

    public PurchaseRequest(int id, int money) {
        this.id = id;
        this.money = money;
    }

    public int getMoney() {
        return money;
    }

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

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

 

⑦、Clinet

package org.example.shiyanshiwu;

public class Client {
    public static void main(String[] args) {
        Leader objDirector, objDepartmentManager, objManager, objGeneralManager;

        objDirector = new Director("张三");
        objDepartmentManager = new DepartmentManager("李四");
        objManager = new Manager("王五");
        objGeneralManager = new GeneralManager("赵六");

        objDirector.setSuccessor(objDepartmentManager);
        objDepartmentManager.setSuccessor(objManager);
        objManager.setSuccessor(objGeneralManager);

        PurchaseRequest pr1 = new PurchaseRequest(1001, 10000);
        objDirector.handleRequest(pr1);

        PurchaseRequest pr2 = new PurchaseRequest(1002, 50000);
        objDirector.handleRequest(pr2);

        PurchaseRequest pr3 = new PurchaseRequest(1003, 100000);
        objDirector.handleRequest(pr3);

        PurchaseRequest pr4 = new PurchaseRequest(1004, 200000);
        objDirector.handleRequest(pr4);

        PurchaseRequest pr5 = new PurchaseRequest(1005, 200001);
        objDirector.handleRequest(pr5);
    }
}

 

3、运行结果

 

 

posted @ 2024-11-13 23:22  七安。  阅读(1)  评论(0编辑  收藏  举报