职责链模式——C++实现

问题描述:

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

类图:

 

 代码:

#include<iostream>
using namespace std;
#include<string>
//抽象管理者
class Manager
{
protected:
    Manager *m_manager;
    string m_name;
public:
    Manager(Manager *manager, string name) :m_manager(manager), m_name(name){}
    virtual void DealWithRequest(string name, int num)  {}
};
//主任
class Zhuren : public Manager
{
public:
    Zhuren(Manager *manager, string name) :Manager(manager, name) {}
    void DealWithRequest(string name, int num)
    {
        if (num < 10000)
        {
            cout << "主任" << m_name << "同意审批编号为"<<name<<"金额为"<<num<<"元的采购单"<<endl;
        }
        else
        {
            cout << "主任" << m_name << "无法处理,交由部门经理处理" << endl;
            m_manager->DealWithRequest(name, num);
        }
    }
};
//部门经理
class Bumen : public Manager
{
public:
    Bumen(Manager *manager, string name) :Manager(manager, name) {}
    void DealWithRequest(string name, int num)
    {
        if (num < 50000)
        {
            cout << "部门经理" << m_name << "同意审批编号为"<<name<<"金额为"<<num<<"元的采购单"<<endl;
        }
        else
        {
            cout << "部门经理" << m_name << "无法处理,交由副总经理处理" << endl;
            m_manager->DealWithRequest(name, num);
        }
    }
};
//副总经理
class  Fuzong : public Manager
{
public:
    Fuzong(Manager *manager, string name) :Manager(manager, name) {}
    void DealWithRequest(string name, int num)
    {
        if (num < 100000)
        {
            cout << "副总经理" << m_name << "同意审批编号为"<<name<<"金额为"<<num<<"元的采购单"<<endl;
        }
        else
        {
            cout << "副总经理" << m_name << "无法处理,交由总经理处理" << endl;
            m_manager->DealWithRequest(name, num);
        }
    }
};
//总经理
class  Zong : public Manager
{
public:
    Zong(Manager *manager, string name) :Manager(manager, name) {}
    void DealWithRequest(string name, int num)
    {
        if (num < 200000)
        {
            cout << "总经理" << m_name << "同意审批编号为"<<name<<"金额为"<<num<<"元的采购单"<<endl;
        }
        else
        {
            cout << "总经理" << m_name << "无法处理,召开职工大会处理" << endl;
            m_manager->DealWithRequest(name, num);
        }
    }
};
class Congress : public Manager
{
public:
    Congress(Manager *manager, string name) :Manager(manager, name) {}
    void DealWithRequest(string name, int num)
    {
        cout << "职工大会" << m_name << "审批采购单:" << name << "同意审批编号为"<<name<<"金额为"<<num<<"元的采购单"<<endl;
    }
};
//主函数
int main()
{
    //设置上级
    Manager *congress = new Congress(NULL, "");
    Manager *zong = new Zong(congress, "赵六");
    Manager *fuzong = new Fuzong(zong, "王五");
    Manager *bumen = new Bumen(fuzong, "李四");
    Manager *zhuren = new Zhuren(bumen, "张三");
    zhuren->DealWithRequest("001", 5000);
    cout<<"-----------------------------------------"<<endl;
    zhuren->DealWithRequest("002", 10000);
    cout<<"-----------------------------------------"<<endl;
    zhuren->DealWithRequest("003", 60000);
    cout<<"-----------------------------------------"<<endl;
    zhuren->DealWithRequest("004", 110000);
    cout<<"-----------------------------------------"<<endl;
    zhuren->DealWithRequest("005", 300000);
    cout<<"-----------------------------------------"<<endl;
    delete zhuren;
    delete bumen;
    delete fuzong;
    delete zong;
    delete congress;
    system("pause");
    return 0;
}

  运行截图:

 

posted @ 2022-11-12 22:43  好(justice)……  阅读(15)  评论(0编辑  收藏  举报