zoukankan      html  css  js  c++  java
  • 设计模式 C++实现职责链模式 (顺便复习C++)

    #include <iostream>
    #include <list>
    #include<algorithm>
    #include<sstream>
    using namespace std;
    class PurchaseRequest 
    {
    private:
        string purchaseName;
        int purchaseNum;
    public:
        PurchaseRequest(string purchaseName, int purchaseNum) 
        {
            this->purchaseName = purchaseName;
            this->purchaseNum = purchaseNum;
        }
        int getPurchaseNum() 
        {
            return purchaseNum;
        }
        void setPurchaseName(string purchaseName)
        {
            this->purchaseName = purchaseName;
        }
        string getPurchaseName() {
            return purchaseName;
        }
        void setPurchaseNum(int purchaseNum) {
            this->purchaseNum = purchaseNum;
        }
    };
    class Leader 
    {
    //C++中类中自身类的对象不能做自己的数据成员,但是做静态数据成员、引用类数据成员等可以
    //不能使用抽象数据成员
    protected:
        string name;
        Leader *successor;//使用指针,类的定义要知道类的空间分配大小
    public:
        Leader(string name)
        {
            this->name = name;
        }
        Leader(Leader* leader, string name) :successor(leader), name(name) {}//:冒号,参数初始化
        virtual void handleRequest(PurchaseRequest request) = 0;
    };
    class Director: public Leader
    {
    public:
        Director(Leader* leader, string name): Leader(leader,name){}//子类调用父类的构造函数
        void handleRequest(PurchaseRequest requset) 
        {
            int num = requset.getPurchaseNum();
            string num_str;
            stringstream sStream;
            sStream << num;
            sStream >> num_str;
            if (requset.getPurchaseNum() <= 10000) {
                cout << "主任" + name + "审批" + requset.getPurchaseName() + "" + num_str+ "元采购单" << endl;
            }
            else {
                successor->handleRequest(requset);//交由上级处理
            }
        }
    };
    class Manager :public Leader
    {
    public:
        Manager(Leader* leader, string name) : Leader(leader, name) {}//子类调用父类的构造函数
        void handleRequest(PurchaseRequest requset)
        {
            int num = requset.getPurchaseNum();
            //int类型转为string类型
            string num_str;
            stringstream sStream;
            sStream << num;
            sStream >> num_str;
            if (requset.getPurchaseNum() <= 50000) {
                cout << "部门经理" + name + "审批" + requset.getPurchaseName() + "" + num_str + "元采购单" << endl;
            }
            else {
                successor->handleRequest(requset);//交由上级处理
            }
        }
    };
    class smallGeneralManager :public Leader
    {
    public:
        smallGeneralManager(Leader* leader, string name) : Leader(leader, name) {}//子类调用父类的构造函数
        void handleRequest(PurchaseRequest requset)
        {
            int num = requset.getPurchaseNum();
            string num_str;
            stringstream sStream;
            sStream << num;
            sStream >> num_str;
            if (requset.getPurchaseNum() <= 100000) {
                cout << "副总经理" + name + "审批" + requset.getPurchaseName() + "" + num_str + "元采购单" << endl;
            }
            else {
                successor->handleRequest(requset);//交由上级处理
            }
        }
    };
    class GeneralManager :public Leader
    {
    public:
        GeneralManager(Leader* leader, string name) : Leader(leader, name) {}//子类调用父类的构造函数
        void handleRequest(PurchaseRequest requset)
        {
            int num = requset.getPurchaseNum();
            string num_str;
            stringstream sStream;
            sStream << num;
            sStream >> num_str;
            if (requset.getPurchaseNum() <= 200000) {
                cout << "总经理" + name + "审批" + requset.getPurchaseName() + "" + num_str + "元采购单" << endl;
            }
            else {
                successor->handleRequest(requset);//交由上级处理
            }
        }
    };
    class Workers :public Leader
    {
    public:
        Workers(Leader* leader, string name) : Leader(leader, name) {}//子类调用父类的构造函数
        void handleRequest(PurchaseRequest requset)
        {
            int num = requset.getPurchaseNum();
            string num_str;
            stringstream sStream;
            sStream << num;
            sStream >> num_str;
            if (requset.getPurchaseNum() > 200000) {
                cout << "经职工大会" + name + "审批" + requset.getPurchaseName() + "" + num_str + "元采购单" << endl;
            }
            else {
                successor->handleRequest(requset);//交由上级处理
            }
        }
    };
    int main()
    {
        Leader* workers = new Workers(NULL, "第一次会议");
        Leader* generalManger = new GeneralManager(workers, "张三");
        Leader* objectsmallGeneralManager = new smallGeneralManager(generalManger, "李波");
        Leader* objectManager = new  Manager(objectsmallGeneralManager, "赵强");
        Leader* objectDirector = new Director(objectManager, "王明");
        PurchaseRequest purchaseRequest1("小红", 5000);
        PurchaseRequest purchaseRequest2("小绿", 40000);
        PurchaseRequest purchaseRequest3("小黄", 80000);
        PurchaseRequest purchaseRequest4("小蓝", 150000);
        PurchaseRequest purchaseRequest5("小紫", 250000);
        objectDirector->handleRequest(purchaseRequest1);
        objectDirector->handleRequest(purchaseRequest2);
        objectDirector->handleRequest(purchaseRequest3);
        objectDirector->handleRequest(purchaseRequest4);
        objectDirector->handleRequest(purchaseRequest5);
    }
  • 相关阅读:
    http协议
    web应用
    前端基础-jquery
    jQuery的事件
    2.UML类图基本介绍
    1.设计模式的七大原则
    使用OpenFeign远程调用时请求头处理报错问题
    SpringCloud Config-分布式配置中心
    19. 类加载器详解
    18. 类加载过程详解
  • 原文地址:https://www.cnblogs.com/fengchuiguobanxia/p/15559612.html
Copyright © 2011-2022 走看看