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;
    }
  • 相关阅读:
    Android关机流程源码分析
    开关机画面
    android camera
    深入分析AIDL原理
    VMware 虚拟机里连不上网的三种解决方案
    查看Linux中自带的jdk ,设置JAVA_HOME
    Hadoop三种模式安装教程(详解)
    Java ArrayList遍历删除问题
    idea git的使用(四)git建立分支与合并分支
    SpringBoot非官方教程 | 终章:文章汇总
  • 原文地址:https://www.cnblogs.com/tiandsp/p/2565629.html
Copyright © 2011-2022 走看看