設計模式--組合模式Composite(結構性)

組合模式(Composite)應用背景

在繪圖編輯器的應用程序中,用戶可以使用簡單圖形組件創建複雜的圖表,也可以組合多個簡單圖形形成較大的組件,這些新產生圖形組件又可以組合成爲更大的組件,如此產生不斷遞歸層次。類似情況也有在文件系統中,一個目錄中可以包含單個獨立文件和子文件夾,子文件夾中又可以包含單個獨立文件和子文件夾等等。 軟件設計中,針對這種“部分–整體”的層次結構,提出了組合模式描述。

組合模式將對象組合成樹形結構以表示“部分–整體”的層次結構,組合對象與基本對象擁有相同的接口,用戶對單個對象和組合對象的使用具有一致性,不必關心到底是處理一個單個對象還是處理一個組合對象。簡單可以理解爲:是把一些現有的對象或者元素,經過組合後組成新的對象,新的對象提供內部方法,可以很方便的完成這些元素或者內部對象的訪問和操作,也可以把組合對象理解成一個容器,容器提供各種訪問其內部對象或者元素的API,用戶只需要使用這些方法即可。

組合模式(Composite)UML圖

這裏寫圖片描述

組合模式(Composite)代碼示例

class Component
{
public:
    Component(std::string name):m_strName(name){};
    virtual ~Component(){};

public:
    virtual void Operation() = 0;
    virtual void Add(Component* pObj) = 0;
    virtual void Remove(Component* pObj) = 0;
    virtual Component* GetChild(int index) = 0;
    virtual std::string GetName()
    {
        return m_strName;
    }

protected:
    std::string m_strName;
};


class Leaf: public Component
{
public:
    Leaf(std::string name): Component(name){ }
    ~Leaf(){}

public:
    void Operation()
    {
        //TODO:
    }
    void Add(Component* pObj)
    { 
        //leaf, no need realization
    };
    void Remove(Component* pObj)
    { 
        //leaf, no need realization
    };
    Component* GetChild(int index)
    { 
        return NULL;  //leaf, no need realization
    };
};

class Composite: public Component
{
public:
    Composite(std::string name):Component(name){}
    ~Composite()
    {
        std::vector<Component *>::iterator itor = m_vecComponent.begin();
        while (itor != m_vecComponent.end())
        {
            if (*itor != NULL)
            {   
                delete *itor;
                *itor = NULL;
            }
            m_vecComponent.erase(itor);
            itor = m_vecComponent.begin();
        }
    }

private:
    std::vector<Component*> m_vecComponent;

public:
    void Operation()
    {
        //TODO:
    }

    void Add(Component* pObj)
    {
        m_vecComponent.push_back(pObj);
    }

    void Remove(Component* pObj)
    {
        std::vector<Component*>::iterator itor=m_vecComponent.begin();
        for( ; itor!=m_vecComponent.end(); ++itor)
        {
            if((*itor)->GetName() == pObj->GetName())
            {
                if (*itor != NULL)
                {   
                    delete *itor;
                    *itor = NULL;
                }
                m_vecComponent.erase(itor);
                break;
            }
        }
    }

    Component* GetChild(int index)
    {
        if(index > m_vecComponent.size())
            return NULL;

        return m_vecComponent[index-1];
    }
};

//Client usage example:
Component *pRootNode = new Composite("Head Office");
Component *pNodeHR = new Leaf("Head Office HR");
Component *pSubNodeSH = new Composite("SH Office");
Component *pSubNodeXA = new Composite("XA Office");
Component *pSubNodeHK = new Composite("HK Office");
pRootNode->Add(pNodeHR);
pRootNode->Add(pSubNodeSH);
pRootNode->Add(pSubNodeXA);
pRootNode->Add(pSubNodeHK);
pRootNode->Operation();

Component *pSubNodeSHHR = new Leaf("Shanghai HR Office");     
Component *pSubNodeSHSS = new Leaf("Shanghai Sales Office");
Component *pSubNodeSHRD = new Leaf("Shanghai RD Office");
pSubNodeSH->Add(pSubNodeSHHR);
pSubNodeSH->Add(pSubNodeSHSS);
pSubNodeSH->Add(pSubNodeSHRD);
pRootNode->Operation();

pSubNodeSH->Remove(pSubNodeSHRD);
if (pSubNodeSHRD != NULL)
{
    delete pSubNodeSHRD;
    pSubNodeSHRD = NULL;
}

注意事項:
使用中需要特別注意內存的釋放,由於組合模式中存在樹形結構,當父節點都被銷燬時,所有的子節點也必須被銷燬,因此,代碼示例使用在析構函數中對維護的Component列表進行統一銷燬,這樣就可以免去使用者頻繁銷燬子節點的麻煩。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章