职责链模式

财务审批

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

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

}

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

            }

        }

    }

}

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

            }

        }

    }

}

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() + "元,金额太大,需开职工大会确定是否审批通过");

        }

    }

}

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

            }

        }

    }

}

public class PurchaseRequest

{

    private int money;

    private int id;

    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;

    }

}

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

    }

posted @ 2024-11-22 15:02  涨涨涨张  阅读(4)  评论(0编辑  收藏  举报