zoukankan      html  css  js  c++  java
  • 责任链模式(C++)

    #include <iostream>
    #include <vector>
    using namespace std;
    
    class relatives
    {
    public:
        relatives(){}
        virtual ~relatives(){}
        virtual bool request(int)=0;
    };
    
    class brother : public relatives
    {
    public:
        brother(){}
        virtual ~brother(){}
        bool request(int num)
        {
            if (num<100)
            {
                cout<<"哥哥给你买"<<endl;
                return true;
            }
            else
            {
                cout<<"哥哥不给买,让妈妈买吧"<<endl;
                return false;
            }
        }
    };
    
    class mother : public relatives
    {
    public:
        mother(){}
        virtual ~mother(){}
        bool request(int num)
        {
            if (num<500)
            {
                cout<<"妈妈给你买"<<endl;
                return true;
            }
            else
            {
                cout<<"妈妈不给买,让爸爸买吧"<<endl;
                return false;
            }
        }
    };
    
    class father : public relatives
    {
    public:
        father(){}
        virtual ~father(){}
        bool request(int num)
        {
            if (num<1000)
            {
                cout<<"爸爸给你买"<<endl;
                return true;
            }
            else
            {
                cout<<"爸爸不给买,...."<<endl;
                return false;
            }
        }
    };
    
    class buysomething
    {
    private:
        vector<relatives*> p_vbuy;
    public:
        buysomething()
        {
            p_vbuy.push_back(new brother);
            p_vbuy.push_back(new mother);
            p_vbuy.push_back(new father);
        }
        virtual ~buysomething()
        {
            p_vbuy.clear();
        }
        void request(int num)
        {
            bool flag=false;
            for (vector<relatives*>::iterator it=p_vbuy.begin();it!=p_vbuy.end();it++)
            {
                flag=(*it)->request(num);
                if (flag==true)
                    break;
            }
        }
    };
    
    int main()
    {
        buysomething *buy=new buysomething;
        buy->request(6000);
        delete buy;
    
        system("pause");
        return 0;
    }
  • 相关阅读:
    一个datagrid中嵌入checkBox典型的例子
    堆排序算法
    堆排序(利用最大堆)
    动态代理模式的实现
    [转载]C#如何实现对外部程序的动态调用
    9.Jmeter 多个threadgroup 中的配置元件会一次性进行初始化
    二十七。
    三十。接口2
    三十三。日志
    大道至简读后感
  • 原文地址:https://www.cnblogs.com/tiandsp/p/2565629.html
Copyright © 2011-2022 走看看