作用:
將對(duì)象組合成樹(shù)形結(jié)構(gòu)以表示“部分-整體”的層次結(jié)構(gòu)。Composite使得用戶對(duì)單個(gè)對(duì)象和組合對(duì)象的使用具有一致性。
UML結(jié)構(gòu)圖:

抽象基類:
1)Component:為組合中的對(duì)象聲明接口,聲明了類共有接口的缺省行為(如這里的Add,Remove,GetChild函數(shù)),聲明一個(gè)接口函數(shù)可以訪問(wèn)Component的子組件.
接口函數(shù):
1)Component::Operatation:定義了各個(gè)組件共有的行為接口,由各個(gè)組件的具體實(shí)現(xiàn).
2)Component::Add添加一個(gè)子組件
3)Component::Remove::刪除一個(gè)子組件.
4)Component::GetChild:獲得子組件的指針.
解析:
Component模式是為解決組件之間的遞歸組合提供了解決的辦法,它主要分為兩個(gè)派生類,其中的Leaf是葉子結(jié)點(diǎn),也就是不含有子組件的結(jié)點(diǎn),而Composite是含有子組件的類.舉一個(gè)例子來(lái)說(shuō)明這個(gè)模式,在UI的設(shè)計(jì)中,最基本的控件是諸如Button,Edit這樣的控件,相當(dāng)于是這里的Leaf組件,而比較復(fù)雜的控件比如List則可也看做是由這些基本的組件組合起來(lái)的控件,相當(dāng)于這里的Composite,它們之間有一些行為含義是相同的,比如在控件上作一個(gè)點(diǎn)擊,移動(dòng)操作等等的,這些都可以定義為抽象基類中的接口虛函數(shù),由各個(gè)派生類去實(shí)現(xiàn)之,這些都會(huì)有的行為就是這里的Operation函數(shù),而添加,刪除等進(jìn)行組件組合的操作只有非葉子結(jié)點(diǎn)才可能有,所以虛擬基類中只是提供接口而且默認(rèn)的實(shí)現(xiàn)是什么都不做.
實(shí)現(xiàn):
1)Composite.h

/**//********************************************************************
????created:????2006/07/20
????filename:?????Composite.h
????author:????????李創(chuàng)
????????????????http://www.shnenglu.com/converse/

????purpose:????Composite模式的演示代碼
*********************************************************************/

#ifndef?COMPOSITE_H
#define?COMPOSITE_H

#include?<list>

//?組合中的抽象基類
class?Component


{
public:

????Component()
{}

????virtual?~Component()
{}

????//?純虛函數(shù),只提供接口,沒(méi)有默認(rèn)的實(shí)現(xiàn)
????virtual?void?Operation()?=?0;

????//?虛函數(shù),提供接口,有默認(rèn)的實(shí)現(xiàn)就是什么都不做
????virtual?void?Add(Component*?pChild);
????virtual?void?Remove(Component*?pChild);
????virtual?Component*?GetChild(int?nIndex);
};

//?派生自Component,是其中的葉子組件的基類
class?Leaf
????:?public?Component


{
public:

????Leaf()
{}

????virtual?~Leaf()
{}

????virtual?void?Operation();
};

//?派生自Component,是其中的含有子件的組件的基類
class?Composite
????:?public?Component


{
public:

????Composite()
{}
????virtual?~Composite();

????virtual?void?Operation();

????virtual?void?Add(Component*?pChild);
????virtual?void?Remove(Component*?pChild);
????virtual?Component*?GetChild(int?nIndex);

private:
????//?采用list容器去保存子組件
????std::list<Component*>????m_ListOfComponent;
};

#endif2)Composite.cpp

/**//********************************************************************
????created:????2006/07/20
????filename:?????Composite.cpp
????author:????????李創(chuàng)
????????????????http://www.shnenglu.com/converse/

????purpose:????Composite模式的演示代碼
*********************************************************************/

#include?"Composite.h"
#include?<iostream>
#include?<algorithm>


/**//*-------------------------------------------------------------------
????Component成員函數(shù)的實(shí)現(xiàn)

?-------------------------------------------------------------------*/
void?Component::Add(Component*?pChild)


{

}

void?Component::Remove(Component*?pChild)


{

}

Component*?Component::GetChild(int?nIndex)


{
????return?NULL;
}


/**//*-------------------------------------------------------------------
????Leaf成員函數(shù)的實(shí)現(xiàn)

-------------------------------------------------------------------*/
void?Leaf::Operation()


{
????std::cout?<<?"Operation?by?leaf\n";
}


/**//*-------------------------------------------------------------------
????Composite成員函數(shù)的實(shí)現(xiàn)

-------------------------------------------------------------------*/
Composite::~Composite()


{
????std::list<Component*>::iterator?iter1,?iter2,?temp;

????for?(iter1??=?m_ListOfComponent.begin(),?iter2?=?m_ListOfComponent.end();
?????????iter1?!=?iter2;
?????????)

????
{
????????temp?=?iter1;
????????++iter1;
????????delete?(*temp);
????}
}

void?Composite::Add(Component*?pChild)


{
????m_ListOfComponent.push_back(pChild);
}

void?Composite::Remove(Component*?pChild)


{
????std::list<Component*>::iterator?iter;

????iter?=?find(m_ListOfComponent.begin(),?m_ListOfComponent.end(),?pChild);

????if?(m_ListOfComponent.end()?!=?iter)

????
{
????????m_ListOfComponent.erase(iter);
????}
}

Component*?Composite::GetChild(int?nIndex)


{
????if?(nIndex?<=?0?||?nIndex?>?m_ListOfComponent.size())
????????return?NULL;

????std::list<Component*>::iterator?iter1,?iter2;
????int?i;
????for?(i?=?1,?iter1??=?m_ListOfComponent.begin(),?iter2?=?m_ListOfComponent.end();
????????iter1?!=?iter2;
????????++iter1,?++i)

????
{
????????if?(i?==?nIndex)
????????????break;
????}

????return?*iter1;
}

void?Composite::Operation()


{
????std::cout?<<?"Operation?by?Composite\n";

????std::list<Component*>::iterator?iter1,?iter2;

????for?(iter1??=?m_ListOfComponent.begin(),?iter2?=?m_ListOfComponent.end();
????????iter1?!=?iter2;
????????++iter1)

????
{
????????(*iter1)->Operation();
????}
}3)Main.cpp

/**//********************************************************************
????created:????2006/07/20
????filename:?????Main.cpp
????author:????????李創(chuàng)
????????????????http://www.shnenglu.com/converse/

????purpose:????Composite模式的測(cè)試代碼
*********************************************************************/

#include?"Composite.h"
#include?<stdlib.h>

int?main()


{
????Leaf?*pLeaf1?=?new?Leaf();
????Leaf?*pLeaf2?=?new?Leaf();

????Composite*?pComposite?=?new?Composite;
????pComposite->Add(pLeaf1);
????pComposite->Add(pLeaf2);
????pComposite->Operation();
????pComposite->GetChild(2)->Operation();

????delete?pComposite;

????system("pause");

????return?0;
}