设计模式[14]-Composite

简介:

Type: Structural Composite: 通过递归手段来构造诸如文件系统之类的树形的对象结构;Composite模式所代表的数据构造是一群具有统一接口界面的对象集合,并可以通过一个对象来访问所有的对象(遍历)。


#include <iostream>
#include <vector>
#include <typeinfo> 
using namespace std;

class Component
{
public:
    virtual void operation()=0;
    virtual void add(Component* c){};
    virtual void remove(Component* c){};
    virtual Component* getChild(int i){};
};

class Leaf: public Component
{
public:
    void operation()
    {
        cout<<"Leaf"<<endl;
    };
};

class Composite: public Component
{
public:
    void operation()
    {
        cout<<"Composite"<<endl;
    };
    void add(Component* c)
    {
        mChildren.insert(mChildren.begin(), c);
    };
    void remove(Component* c)
    {        
        vector<Component*>::iterator it;
        for(it = mChildren.begin(); it < mChildren.end(); it++)
            if(*it == c)
            {
                mChildren.erase(it);
                break;
            }                
    };
    Component* getChild(int i)
    {
        if(i < 0 || i >= mChildren.size())
            return NULL;
        return mChildren[i]; 
    };
private:
    vector<Component*> mChildren;
};

void printComponent(Component* pComponent)
{
     pComponent->operation();
     if(!dynamic_cast<Composite*>(pComponent))
         return;     
     int i=0;
     Component* childComponent;
     while(childComponent = pComponent->getChild(i++))
     {
         printComponent(childComponent);
     };
};
int main()
{
    Leaf *pLeaf1 = new Leaf();
    Leaf *pLeaf2 = new Leaf();

    Composite* pComposite = new Composite;
    pComposite->add(pLeaf1);
    pComposite->add(pLeaf2);
    Composite* pComposite2 = new Composite;
    pComposite2->add(pComposite);
    printComponent(pComposite2);

    system("pause");
    return 0;
}

目录
相关文章
|
3月前
|
设计模式
二十三种设计模式全面解析-解密组合模式(Composite Pattern):构建统一而强大的对象结构
二十三种设计模式全面解析-解密组合模式(Composite Pattern):构建统一而强大的对象结构
|
28天前
|
设计模式
对抗软件复杂度问题之组合(Composite)方法设计模式是什么,如何解决
对抗软件复杂度问题之组合(Composite)方法设计模式是什么,如何解决
|
3月前
|
设计模式 存储 Java
认真学习设计模式之组合模式(Composite Pattern)
认真学习设计模式之组合模式(Composite Pattern)
46 0
|
10月前
|
设计模式 Java 容器
【设计模式——学习笔记】23种设计模式——组合模式Composite(原理讲解+应用场景介绍+案例介绍+Java代码实现)
【设计模式——学习笔记】23种设计模式——组合模式Composite(原理讲解+应用场景介绍+案例介绍+Java代码实现)
79 0
【设计模式——学习笔记】23种设计模式——组合模式Composite(原理讲解+应用场景介绍+案例介绍+Java代码实现)
|
10月前
|
设计模式 Java uml
设计模式15 - 组合模式【Composite Pattern】
设计模式15 - 组合模式【Composite Pattern】
24 0
|
10月前
|
设计模式 Java 容器
设计模式~组合模式(composite)-16
目录 (1)优点: (2)缺点: (3)使用场景: (4)注意事项: (5)应用实例 代码
44 0
|
设计模式 Java 数据安全/隐私保护
Java设计模式-组合模式(Composite)
Java设计模式-组合模式(Composite)
|
设计模式 存储 容器
从零开始学设计模式(十一):组合模式(Composite Pattern):
组合模式(Composite Pattern)又叫做部分-整体模式,它在树型结构(可以想象一下数据结构中的树)的问题中,模糊了简单元素和复杂元素的概念,客户端程序可以像处理简单元素一样来处理复杂元素,而使得客户端程序与复杂元素的内部结构进行解藕。
134 0
从零开始学设计模式(十一):组合模式(Composite Pattern):
|
设计模式 存储 容器
【愚公系列】2021年12月 二十三种设计模式(八)-组合模式(Composite Pattern)
【愚公系列】2021年12月 二十三种设计模式(八)-组合模式(Composite Pattern)
107 0
【愚公系列】2021年12月 二十三种设计模式(八)-组合模式(Composite Pattern)
|
存储 设计模式 Java
浅谈JAVA设计模式之——组合模式(Composite)
将对象组合成树形结构以表示"部分-整体"的层次结构。"Composite使得用户对单个对象和组合对象的使用具有一致性。
210 0
浅谈JAVA设计模式之——组合模式(Composite)