首页 > 其他分享 >软件设计-职责链模式

软件设计-职责链模式

时间:2022-10-22 18:44:23浏览次数:47  
标签:name 软件设计 模式 Leader PurchaseRequest handleRequest new public 职责

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

 

类图

 

 

java

   
package rjsj.no15;

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);
}
     
package rjsj.no15;

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;
    }
}
     
package rjsj.no15;

public class Client {
    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("****************************************");
    }
}
     
package rjsj.no15;

//主任
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);
            }
        }
    }
}

//部门经理
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);
            }
        }
    }
}

//副经理
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);
            }
        }
    }
}

//总经理
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() + "元");
        }
    }
}
 

 

C++

   
#include<iostream>
using namespace std;
class PurchaseRequest {
private:
    int money;
public:
    PurchaseRequest(int money) {
        this->money=money;
    }
    void setMoney(int money) {
        this->money=money;
    }
    int getMoney(){
        return this->money;
    }
};
class Leader {
protected:
    string name;
    Leader *successor;
public:
    Leader(string name) {
        this->name=name;
    }
    void setSuccessor(Leader *successor) {
        this->successor=successor;
    }
    virtual void handleRequest(PurchaseRequest *request)=0;
};
class Director:public Leader{
private:
    string name;
public:
    Director(string name):Leader(name){}
    void handleRequest(PurchaseRequest *request) {
        // TODO Auto-generated method stub
        if(request->getMoney()<=1) {
            cout<<"主任"<<this->name<<"审批"<<request->getMoney()<<"万元的采购单"<<endl;
        }
        else {
            if(this->successor!=NULL) {
                this->successor->handleRequest(request);
            }
        }
    }
};
class Manager : public Leader{
private:
    string name;
public:
    Manager(string name):Leader(name){}
    void handleRequest(PurchaseRequest *request) {
        if(request->getMoney()<=5) {
            cout<<"部门经理"<<name<<"审批"<<request->getMoney()<<"万元的采购单"<<endl;
        }
        else {
            if(this->successor!=NULL) {
                this->successor->handleRequest(request);
            }
        }
    }
};
class DeputyGeneralManager: public Leader{
private:
    string name;
public:
    DeputyGeneralManager(string name):Leader(name){}
    void handleRequest(PurchaseRequest *request) {
        if(request->getMoney()<=10) {
            cout<<"副总经理"<<name<<"审批"<<request->getMoney()<<"万元的采购单"<<endl;
        }
        else {
            if(this->successor!=NULL) {
                this->successor->handleRequest(request);
            }
        }
    }
};
class GeneralManager: public Leader{
private:
    string name;
public:
    GeneralManager(string name):Leader(name){}
    void handleRequest(PurchaseRequest *request) {
        if(request->getMoney()<=20) {
            cout<<"总经理"<<name<<"审批"<<request->getMoney()<<"万元的采购单"<<endl;
        }
        else if(request->getMoney()>20) {
            cout<<"总经理"<<name<<"召开职工大会审批"<<request->getMoney()<<"万元的采购单"<<endl;
        }
        else {
            if(this->successor!=NULL) {
                this->successor->handleRequest(request);
            }
        }
    }
};
int main(){
        Leader *objDirector,*objManager,*objDeputyGeneralManager,*objGeneralManager;

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

        objDirector->setSuccessor(objManager);
        objManager->setSuccessor(objDeputyGeneralManager);
        objDeputyGeneralManager->setSuccessor(objGeneralManager);

        PurchaseRequest *pr1=new PurchaseRequest(1);
        objDirector->handleRequest(pr1);
        PurchaseRequest *pr2=new PurchaseRequest(3);
        objDirector->handleRequest(pr2);
        PurchaseRequest *pr3=new PurchaseRequest(8);
        objDirector->handleRequest(pr3);
        PurchaseRequest *pr4=new PurchaseRequest(12);
        objDirector->handleRequest(pr4);
        PurchaseRequest *pr5=new PurchaseRequest(22);
        objDirector->handleRequest(pr5);
    return 0;
}
 

 

运行结果

 


标签:name,软件设计,模式,Leader,PurchaseRequest,handleRequest,new,public,职责
From: https://www.cnblogs.com/libin159/p/16816973.html

相关文章

  • go语言设计模式-单例模式
    GetInstance方法开放了instance变量的读权限,使得只能读到instance,而不能对其赋值varinstance*singelton=new(singelton),饿汉式:在main执行之前,编译期间通过new就已经被......
  • 使用混合模式叠加实现文字波浪小姑
        <!DOCTYPEhtml><htmllang="en"><head>  <metacharset="UTF-8">  <metahttp-equiv="X-UA-Compatible"content="IE=edge">  <metanam......
  • 代理模式
    代理模式静态代理优点业务类只需要关注业务逻辑本身,保证了业务类的重用性。这是代理的共有优点。代理使客户端不需要知道实现类是什么,怎么做的,而客户端只需知道代理即可......
  • 设计模式之工厂方法模式
    简介在简单工厂模式中只提供一个工厂类,该工厂类处于对产品类进行实例化的中心位置,它需要知道每一个产品对象的创建细节,并决定何时实例化哪一个产品类。简单工厂模式最大的......
  • 14*4点 LCD液晶显示驱动控制电路(IC)-VK1S56D SSOP24 0.635脚距,多用于美容仪器/电池电
    产品品牌:永嘉微电/VINKA产品型号:VK1S56D封装形式:SSOP24(脚位间距:0.635mm)概述:VK1S56DSSOP24是一个点阵式存储映射的LCD驱动器,可支持最大56点(14SEGx4COM)的LCD屏,也支......
  • Spring Cloud集成Seata分布式事务-TCC模式
    参考文章分布式事务实战方案汇总https://www.cnblogs.com/yizhiamumu/p/16625677.html分布式事务原理及解决方案案例 https://www.cnblogs.com/yizhiamumu/p/16662412.......
  • 移动光猫改桥接模式--获得移动ipv6公网ip地址
         事实上现今许多网络服务提供商(ISP)都提供了IPV6地址,你手机连上移动、联通、电信的基站,都能获取到一个IPV6地址,没道理宽带不给提供啊。  其实啊,许多发展还不......
  • 代理模式
    婚介所其实就是找对象的一个代理,请仿照我们的课堂例子“论坛权限控制代理”完成这个实际问题,其中如果年纪小于18周岁,婚介所会提示“对不起,不能早恋!”,并终止业务。  ......
  • 一起学习 Go 语言设计模式之建造者模式
    前言你去买车,你不会只买一个轮胎、一个发动机、一个方向盘,你买的是一辆包括轮胎、方向盘、发动机、底盘、电气系统和车身等多个部件组成的完整骑车。在设计模式中,建造者模式......
  • 设计模式专项练习
    A.模版模式:定义一个算法结构,而将一些步骤延迟到子类去实现B.备忘录模式:在不破坏封装的前提下,保持对象的内部状态C.抽象工厂:创建相关或依赖对象的家族,而无需指明具体类D.......