【设计模式】——组合模式

组合模式(Composite),将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。

#include <iostream>
#include <list>
#include <string>
using namespace std;
//Component为组合中的对象声明接口,在适当情况下,实现所有类共有接口的默认行为。
//声明一个接口用于访问和管理Component的子部件
class Component
{
protected:
    string m_name;
public:
    Component(string name):m_name(name)
    {
    }
    virtual void Add(Component *c){}
    virtual void Remove(Component *c){}
    virtual void Display(int depth){}
};
//Leaf在组合中表示叶结点对象,叶结点没有子结点。
class Leaf:public Component
{
public:
    Leaf(string name):Component(name)
    {
    }
    void Add(Component *c)
    {
        cout << "Cannot add to a leaf" << endl;
    }
    void Remove(Component *c)
    {
        cout << "Cannot remove from a leaf" << endl;
    }
    void Display(int depth)
    {
        string strTemp(depth,'-');
        cout << strTemp << m_name << endl;
    }
};
//Composite定义有枝结点行为,用来存储子部件,在Component接口中实现与子部件有关的操作,比如增加和删除
class Composite:public Component
{
private:
    list<Component*> children;
public:
    Composite(string name):Component(name)
    {
    }
    void Add(Component *c)
    {
        children.push_back(c);
    }
    void Remove(Component *c)
    {
        children.remove(c);
    }
    void Display(int depth)
    {
        string strTemp(depth,'-');
        cout << strTemp << m_name << endl;
        list<Component*>::iterator iter=children.begin();
        for(;iter!=children.end();iter++)
            (*iter)->Display(depth+2);
    }
};
int main()
{
    Composite *root=new Composite("root");
    Leaf *leafa=new Leaf("LeafA");
    Leaf *leafb=new Leaf("LeafB");
    root->Add(leafa);
    root->Add(leafb);

    Composite *comp=new Composite("Composite X");
    comp->Add(new Leaf("Leaf XA"));
    comp->Add(new Leaf("Leaf XB"));
    root->Add(comp);

    Composite *comp2=new Composite("Composite XY");
    comp2->Add(new Leaf("Leaf XYA"));
    comp2->Add(new Leaf("Leaf XYB"));
    comp->Add(comp2);

    root->Add(new Leaf("Leaf C"));
    Leaf *lef=new Leaf("Leaf D");
    root->Add(lef);
    root->Remove(lef);

    root->Display(1);
    return 0;
}

  树可能有无数的分枝,但只需要反复用Composte就可以实现树状结构了,但是为什么Leaf类当中也有Add和Remove,树叶不是不可以在长分枝,这种方式叫做透明方式,也就是说在Component中声明所有用来管理子对象的方法,其中包括Add、Remove等。这样实现Component接口的所有子类都具备了Add和Remove。这样做的好处就是叶结点和枝结点对于外界没有区别,他们具备完全一致的行为接口。但问题也很明显,因为Leaf类本身不具备Add()、Remove()方法的功能,所以实现它是没有意义的。所以我们又有另一种方式——安全方式,也就是说在Component接口中不去声明Add和Remove方法,那么子类的Leaf也就不需要去实现它,而是在Composite声明所有用来管理子类对象的方法,这样做就不会出现刚才提到的问题,不过由于不够透明,所以树叶和树枝类将不具有相同的接口,客户端的调用需要做相应的判断,带来了不方便。

  何时使用?当你发现需求中是体现部分和整体层次的结构时,以及你希望用户可以忽略组合对象与单个对象的不同,统一地使用组合结构中的所有对象时,就应该考虑用组合模式了。

example:

公司管理系统

#include <iostream>
#include <list>
#include <string>
using namespace std;
class Company
{
protected:
    string m_company;
public:
    Company(string company)
    {
        this->m_company=company;
    }
    virtual void Add(Company *c){}
    virtual void Remove(Company *c){}
    virtual void Display(int depth){}
    virtual void LineOfDuty(){}
};
//具体公司类 实现接口 树枝结点
class ConcreteCompany:public Company
{
private:
    list<Company*> children;
public:
    ConcreteCompany(string company):Company(company){}
    void Add(Company *c)
    {
        children.push_back(c);
    }
    void Remove(Company *c)
    {
        children.remove(c);
    }
    void Display(int depth)
    {
        string strTemp(depth,'-');
        cout << strTemp << m_company << endl;
        list<Company*>::iterator iter=children.begin();
        for(;iter!=children.end();iter++)
            (*iter)->Display(depth+2);
    }
    void LineOfDuty()
    {
        list<Company*>::iterator iter=children.begin();
        for(;iter!=children.end();iter++)
            (*iter)->LineOfDuty();
    }
};
class HRDepartment:public Company
{
public:
    HRDepartment(string company):Company(company){}
    void Add(Company *c){}
    void Remove(Company *c){}
    void Display(int depth)
    {
        string strTemp(depth,'-');
        cout << strTemp << m_company << endl;
    }
    void LineOfDuty()
    {
        cout << m_company << "员工招聘管理" << endl;
    }
};
class FinanceDepartment:public Company
{
public:
    FinanceDepartment(string company):Company(company){}
    void Add(Company *c){}
    void Remove(Company *c){}
    void Display(int depth)
    {
        string strTemp(depth,'-');
        cout << strTemp << m_company << endl;
    }
    void LineOfDuty()
    {
        cout << m_company << "公司财务收支管理" << endl;
    }
};
int main()
{
    ConcreteCompany *root=new ConcreteCompany("北京总公司");
    root->Add(new HRDepartment("总公司人力资源部"));
    root->Add(new FinanceDepartment("总公司财务部"));

    ConcreteCompany *comp=new ConcreteCompany("上海华东分公司");
    comp->Add(new HRDepartment("华东分公司人力资源部"));
    comp->Add(new FinanceDepartment("华东分公司财务部"));
    root->Add(comp);

    ConcreteCompany *comp1=new ConcreteCompany("南京办事处");
    comp1->Add(new HRDepartment("南京办事处人力资源部"));
    comp1->Add(new FinanceDepartment("南京办事处财务部"));
    comp->Add(comp1);

    ConcreteCompany *comp2=new ConcreteCompany("杭州办事处");
    comp2->Add(new HRDepartment("杭州办事处人力资源部"));
    comp2->Add(new FinanceDepartment("杭州办事处财务部"));
    comp->Add(comp2);

    cout << endl << "结构图";
    root->Display(1);
    cout << endl << "职责:";
    root->LineOfDuty();
    return 0;
}

  组合模式定义了包含基本对象和组合对象的类层次结构。基本对象可以被组合成更复杂的组合对象,而这个组合对象又可以被组合,这样不断的递归下去,客户代码中,任何用到基本对象的地方都可以使用组合对象了。用户是不关心到底是处理一个叶结点还是一个组合组件,也就用不着为定义组合而写一些选择判断语句了。简单说,就是组合模式让客户可以一致地使用组合结构和单个对象。

原文地址:https://www.cnblogs.com/awy-blog/p/3829474.html