首页 > 其他分享 >职责链模式

职责链模式

时间:2023-11-27 22:57:35浏览次数:30  
标签:approver 职责 Approver 模式 ProcessRequest purchaseRequest position GetPrice

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

1.提交类图;

2. 提交源代码;

#include<iostream>
#include<string>
using namespace std;

class PurchaseRequest {
private:
    int type;
    float price;
    int id;

public:
    PurchaseRequest(int type, float price, int id) {
        this->type = type;
        this->price = price;
        this->id = id;
    }

    float GetPrice() {
        return this->price;
    }

    int GetId() {
        return this->id;
    }

    void Print() {
        cout << this->GetId() << "号物资" << this->GetPrice() << "元";
    }
};

class Approver {
protected:
    Approver* approver;
    string position;

public:
    Approver(string position) {
        this->position = position;
    }

    void SetApprover(Approver* approver) {
        this->approver = approver;
    }

    virtual void ProcessRequest(PurchaseRequest* purchaseRequest) = 0;
};

class Director :public Approver {
public:
    Director(string position) :Approver(position) {}

    void SetApprover(Approver *approver) {
        this->approver = approver;
    }

    void ProcessRequest(PurchaseRequest* purchaseRequest) {
        if (purchaseRequest->GetPrice() <= 10000.0)
        {
            cout << "请求编号id =>" << purchaseRequest->GetId() << "   由" << this->position << "处理了" << endl;
        }
        else {
            approver->ProcessRequest(purchaseRequest);
        }
    }
};

class DepartmentManager :public Approver {
public:
    DepartmentManager(string position) :Approver(position) {}

    void SetApprover(Approver* approver) {
        this->approver = approver;
    }

    void ProcessRequest(PurchaseRequest* purchaseRequest) {
        if (purchaseRequest->GetPrice() > 10000.0 && purchaseRequest->GetPrice() <= 50000.0) {
        }
        else {
            approver->ProcessRequest(purchaseRequest);
        }
    }
};

class ViceManager :public Approver {
public:
    ViceManager(string position) :Approver(position) {}

    void SetApprover(Approver* approver) {
        this->approver = approver;
    }

    void ProcessRequest(PurchaseRequest* purchaseRequest) {
        if (purchaseRequest->GetPrice() > 50000.0 && purchaseRequest->GetPrice() <= 100000.0) {
            cout << "请求编号id =>" << purchaseRequest->GetId() << "   由" << this->position << "处理了" << endl;
        }
        else {
            approver->ProcessRequest(purchaseRequest);
        }
    }
};

class GeneralManager :public Approver {
public:
    GeneralManager(string position) :Approver(position) {}

    void SetApprover(Approver* approver) {
        this->approver = approver;
    }

    void ProcessRequest(PurchaseRequest* purchaseRequest) {
        if (purchaseRequest->GetPrice() > 100000.0 && purchaseRequest->GetPrice() <= 200000.0) {
            cout << "请求编号id =>" << purchaseRequest->GetId() << "   由" << this->position << "处理了" << endl;

        }
        else {
            cout << "需要开员工大会决定" << endl;
        }
    }
};

int main() {
    PurchaseRequest* purchaseRequest1 = new PurchaseRequest(1, 9000, 1);
    PurchaseRequest* purchaseRequest2 = new PurchaseRequest(1, 49000, 2);
    PurchaseRequest* purchaseRequest3 = new PurchaseRequest(1, 990                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  00, 3);
    PurchaseRequest* purchaseRequest4 = new PurchaseRequest(1, 190000, 4);
    PurchaseRequest* purchaseRequest5 = new PurchaseRequest(1, 210000, 5);

    Director* director = new Director("主任");
    DepartmentManager* departmentManager = new DepartmentManager("部门经理");
    ViceManager* viceManager = new ViceManager("副总经理");
    GeneralManager* generalManager = new GeneralManager("总经理");

    director->SetApprover(departmentManager);
    departmentManager->SetApprover(viceManager);
    viceManager->SetApprover(generalManager);

    cout << purchaseRequest1->GetId() << "金额" << purchaseRequest1->GetPrice() << endl;
    director->ProcessRequest(purchaseRequest1);
    cout << "---------------------------------------" << endl;

    cout << purchaseRequest2->GetId() << "金额" << purchaseRequest2->GetPrice() << endl;
    director->ProcessRequest(purchaseRequest2);
    cout << "---------------------------------------" << endl;
    
    cout << purchaseRequest3->GetId() << "金额" << purchaseRequest3->GetPrice() << endl;
    director->ProcessRequest(purchaseRequest3);
    cout << "---------------------------------------" << endl;

    cout << purchaseRequest4->GetId() << "金额" << purchaseRequest4->GetPrice() << endl;
    director->ProcessRequest(purchaseRequest4);
    cout << "---------------------------------------" << endl;

    cout << purchaseRequest5->GetId() << "金额" << purchaseRequest5->GetPrice() << endl;
    director->ProcessRequest(purchaseRequest5);
    cout << "---------------------------------------" << endl;
}


标签:approver,职责,Approver,模式,ProcessRequest,purchaseRequest,position,GetPrice
From: https://www.cnblogs.com/yzx-sir/p/17860758.html

相关文章

  • 享元模式
    [实验任务一]:围棋设计一个围棋软件,在系统中只存在一个白棋对象和一个黑棋对象,但是它们可以在棋盘的不同位置显示多次。importjava.util.*;//坐标类:外部状态类classCoordinates{privateintx;privateinty;publicCoordinates(intx,inty){this.......
  • Rancher五--在SELinux模式下安装Rancher-RHEL/CentOS
    在SELinux模式下安装Rancher-RHEL/CentOS从1.6+版本后支持为了使Rancher在RHEL/CentOS的SELinux模式下正常工作,您需要在安装有RPM包container-selinux-2.14(或更高的版本)的主机上运行RancherServer容器。同时,全部的agent主机也要安装这个包。如果您安装了较低版本的container-sel......
  • Idea中使用Debug模式​详解
    Idea中使用Debug模式Debug用来追踪代码的运行流程,通常在程序运行过程中出现异常,启用Debug模式可以分析定位异常发生的位置,以及在运行过程中参数的变化。通常我们也可以启用Debug模式来跟踪代码的运行流程去学习三方框架的源码。Debug开篇首先看下IDEA中Debug模式下的界面。如下是在......
  • Kafka3.6.0 linux 安装,非zookeeper模式
    参考:安装Kafka3.3.1及kraft运行,告别zookeeper_kafka3.3.1jdk无需zookeeper,kafka3.0版本集群安装实战 操作系统:ubutun18.04LTS1.下载到kafka官网下载2.上传到服务器指定位置3.解压到指定目录,这里我放到/opt下tar-xzvfkafka_2.13-3.6.0.tgz-C/opt/4.修改配置:因为......
  • GMAC网卡Fixed-Link模式
    GMAC网卡Fixed-Link模式GMACfixed-link固定链接模式,mac与对端的连接方式是写死的,通常用于mactomac(不排除mactophy的情况)。内核要支持fixed-link模式,需要打开CONFIG_FIXED_PHY配置。社区版linux的gmac网卡platform平台驱动中,在设备树默认下支持fixed-link,gmac网卡platform平......
  • cocos creator中的MVC设计模式
    MVC模式非常得火,今天小编就来说说Unity或cocoscreator这类可视化+组件化开发的工具怎么来做MVC。MVC中的M指的是Model,V指的是视图,C指的是控制,接下来我们通过view,Ctrl,Model依次来说明我们基于unity/cocoscreator这类工具的MVC的开发框架中通常是怎么处理的。v......
  • 观察者模式--Java实现
    相关类图具体代码//Observer.javapackageorg.example.test019;publicinterfaceObserver{publicvoidbuy();publicvoidcry();}//Subject.javapackageorg.example.test019;importjava.util.ArrayList;publicabstractclassSubject{protect......
  • 状态模式--Java实现
    具体代码//Account.javapackageorg.example.test020;publicclassAccount{publicAccountStategetAccountState(){returnaccountState;}publicvoidsetAccountState(AccountStateaccountState){this.accountState=accountState;......
  • 设计模式实验
    软件设计                 石家庄铁道大学信息学院 实验21:观察者模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解观察者模式的动机,掌握该模式的结构;2、能够利用观察者模式解决实际问题。    [实验任务一]:股票提醒......
  • 策略模式--Java实现
    具体代码//Travel.javapackageorg.example.test021;publicinterfaceTravel{publicvoidtravel();}//Person.javapackageorg.example.test021;publicclassPerson{publicTravelgetTravel(){returntravel;}publicvoidsetTrav......