composite 组合模式

来源:互联网 发布:c语言分割字符串函数 编辑:程序博客网 时间:2024/06/01 09:26
#include <iostream>#include <string>#include <vector>using namespace std;class component{public:    virtual ~component(){}    virtual void operation() =0;    virtual void add(component *) =0;    virtual void remove(component *) =0;    virtual component *get_child(int ) =0;};class composite : public component{public:    void operation()    {        for (vector<component*>::iterator it = coms.begin();            it != coms.end(); ++it)        {            (*it)->operation();        }    }    void add(component *com)    {        coms.push_back(com);    }    void remove(component *com)    {        for (vector<component*>::iterator it = coms.begin();            it != coms.end(); ++it)        {            if ((*it) == com)            {                coms.erase(it);                break;            }        }    }    component *get_child(int index)    {        return coms[index];    }private:    vector<component*> coms;};class leaf : public component{public:    void operation()    {        cout<< "leaf::operation()"<<endl;    }private:    void add(component *com){}    void remove(component *com){}    component *get_child(int index){}};int main(){    leaf *l = new leaf;    l->operation();    composite *com = new composite();    com->add(l);    com->operation();    component *ll = com->get_child(0);    ll->operation();    return 0;}///////////////////////////////////////////#include <iostream>#include <string>#include <vector>using namespace std;class company{public:    company(string name):m_name(name){}    virtual ~company(){}    virtual void add(company *c) =0;    virtual void remove(company *c) =0;    virtual void display(int depth) =0;    virtual void line_of_duty() =0;protected:    string m_name;};class concrete_company : public company{public:    concrete_company(string name):company(name){}    ~concrete_company()    {        for (vector<company*>::iterator it = coms.begin();            it != coms.end(); ++it)        {            delete *it;        }            }    void add(company *c)    {        coms.push_back(c);    }    void remove(company *c)    {        for (vector<company*>::iterator it = coms.begin();            it != coms.end(); ++it)        {            if ( (*it) == c)            {                coms.erase(it);                break;            }        }    }    void display(int depth)    {        for (int i = 0; i < depth; ++i)            cout<<"-";        cout<<m_name<<endl;        for (vector<company*>::iterator it = coms.begin();            it != coms.end(); ++it)        {            (*it)->display(depth + 2);        }    }    void line_of_duty()    {        for (vector<company*>::iterator it = coms.begin();            it != coms.end(); ++it)            (*it)->line_of_duty();    }private:    vector<company*> coms;        };class hr_department : public company{public:    hr_department(string name):company(name){}    void display(int depth)    {        for (int i = 0; i < depth; ++i)            cout << "-";         cout<<m_name<<endl;    }    void line_of_duty()    {        cout << m_name << " 员工招聘培训管理"<<endl;    }private:    void add(company *c){}    void remove(company *c){}};class finace_department: public company{public:    finace_department(string name):company(name){}    void display(int depth)    {        for (int i = 0; i < depth; ++i)            cout << "-";         cout<<m_name<<endl;    }    void line_of_duty()    {        cout <<m_name << " 公司财务收支管理" <<endl;    }private:    void add(company *c){}    void remove(company *c){}};int main(){    concrete_company root("北京总公司");    root.add(new hr_department("总公司人力资源部"));    root.add(new finace_department("总公司财务部"));    concrete_company comp("上海华东分公司");    comp.add(new hr_department("华东分公司人力资源部"));    comp.add(new finace_department("华东分公司财务部"));    root.add(&comp);    concrete_company comp1("南京办事处");    comp1.add(new hr_department("南京办事处人力资源部"));    comp1.add(new finace_department("南京办事处财务部"));    root.add(&comp1);    concrete_company comp2("杭州办事处");    comp2.add(new hr_department("杭州办事处人力资源部"));    comp2.add(new finace_department("杭州办事处财务部"));    root.add(&comp2);    cout << "结构图" <<endl;    root.display(1);        cout<<"职责"<<endl;    root.line_of_duty();    cout << "end" <<endl;    return 0;}

0 0
原创粉丝点击