【C++设计模式】组合模式

作者 : 开心源码 本文共2845个字,预计阅读时间需要8分钟 发布时间: 2022-05-13 共329人阅读
/*小鱼号的代码日志 * C++设计模式 *  组合模式 *  创立了对象组的树形结构 *  将对象组合成树状结构以表示 *  整体-部分的层次关系 *  要处理的问题: *  当我们要解决的对象可以是 *  生成一颗树形结构,而我们 * 要对树上的节点和叶子节点进行操作时 * 它能够提供一致的方式,而不同考虑 * 它时节点还是叶子*/#include<iostream>#include<string>#include<list>using namespace std;///组织单位class OrganizationComponent{public:    OrganizationComponent(string strName,string strDesc)    {        m_strName = strName;        m_strDesc = strDesc;    }    virtual string showInfo() = 0;    virtual void add(OrganizationComponent* component)    {    }    virtual void remove(OrganizationComponent* component)    {    }protected:    void setName(const string& strName)    {        m_strName = strName;    }    string getName() const    {        return m_strName;    }    void setDesc(const string&  strDesc)    {        m_strDesc = strDesc;    }    string getDesc() const    {        return m_strDesc;    }private:    string m_strName;    string m_strDesc;};///组织单位 - 大学class University :public OrganizationComponent{public:    University(string strName,string strDesc)            :OrganizationComponent(strName,strDesc)    {    }    virtual void add(OrganizationComponent* component)    {        m_comList.push_back(component);    }    virtual void remove(OrganizationComponent* component)    {        m_comList.remove(component );    }    virtual string showInfo()    {        cout << "university name: " << getName() << " desc: " << getDesc() << endl;        for(auto it = m_comList.begin();it!=m_comList.end();it++)        {            (*it)->showInfo();        }    }private:    list<OrganizationComponent*> m_comList;};///组织单位 - 学院class College :public OrganizationComponent{public:    College(string strName,string strDesc)            :OrganizationComponent(strName,strDesc)    {    }    virtual void add(OrganizationComponent* component)    {        m_comList.push_back(component);    }    virtual void remove(OrganizationComponent* component)    {        m_comList.remove(component );    }    virtual string showInfo()    {        cout << "College name: " << getName() << " desc: " << getDesc() << endl;        for(auto it = m_comList.begin();it!=m_comList.end();it++)        {            (*it)->showInfo();        }    }private:    list<OrganizationComponent*> m_comList;};///组织单位 - 系 最小单位class Department :public OrganizationComponent{public:    Department(string strName,string strDesc)            :OrganizationComponent(strName,strDesc)    {    }    virtual string showInfo()    {        cout << "Department name: " << getName() << " desc: " << getDesc() << endl;    }};void testComposition(){    cout << "composition model" << endl;    OrganizationComponent* QingHuaUniversity = new University("Tqinghua university","china top1 university");    OrganizationComponent* ComputerCollege = new College("computer college","china top1 computer college");    OrganizationComponent* MathCollege = new College("math college","china top1 math college");    OrganizationComponent* SoftDepartment = new Department("soft department","soft is no 1");    OrganizationComponent* NetDepartment = new Department("net department","net is perfect");    OrganizationComponent* MathEduDepartment = new Department("math Education department","math is perfect");    QingHuaUniversity->add(ComputerCollege);    QingHuaUniversity->add(MathCollege);    ComputerCollege->add(SoftDepartment);    ComputerCollege->add(NetDepartment);    MathCollege->add(MathEduDepartment);    QingHuaUniversity->showInfo();    cout << "---------------------"<<endl;    ComputerCollege->remove(NetDepartment);    QingHuaUniversity->showInfo();}
说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 【C++设计模式】组合模式

发表回复