Composite(组合)--对象结构型模式

1.意图

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

2.动机

    可以组合多个简单组件以形成一些较大的组件,这些组件又可以组合成更大的组件。Composite模式描述了如何使用递归组合,使得用户不必对这些类进行区别。

3.适用性

  • 表示对象的部分-整体层次结构。
  • 希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。

4.结构

    

    5.代码实例

    

#include <memory>
#include <vector>

class Graphic
{
public:
    Graphic(std::shared_ptr<Graphic> pParent);
    virtual void Add(std::shared_ptr<Graphic>& pGraphic);
    virtual void Remove(std::shared_ptr<Graphic>& pGraphic);
    virtual std::shared_ptr<Graphic> GetChild(int iIndex);
    virtual std::shared_ptr<Graphic>& GetParent();
    virtual void Opereate();

protected:
    std::shared_ptr<Graphic> m_pParent;

    std::vector<std::shared_ptr<Graphic>> m_vecChildrenGraphics;

}; 

class Circle : public Graphic
{
public:
    Circle(std::shared_ptr<Graphic> pParent);
    void Opereate();
};


class Retangle : public Graphic
{
public:
    Retangle(std::shared_ptr<Graphic> pParent);
    void Opereate();
};


class Line : public Graphic
{
public:
    Line(std::shared_ptr<Graphic> pParent);
    void Opereate();
};

class Picture : public Graphic
{
public:
    Picture(std::shared_ptr<Graphic> pParent);
    virtual void Add(std::shared_ptr<Graphic>& pGraphic);
    virtual void Remove(std::shared_ptr<Graphic>& pGraphic);
    virtual std::shared_ptr<Graphic> GetChild(int iIndex);
    void Opereate();
};
#include "Graphic.h"
#include <iostream>


Graphic::Graphic(std::shared_ptr<Graphic> pParent) 
    : m_pParent(pParent)
{
}

 void Graphic::Add(std::shared_ptr<Graphic>& pGraphic)
 {
     std::cout<< "Leaf Cannot Add Childrren" << std::endl;
 }

 void Graphic::Remove(std::shared_ptr<Graphic>& pGraphic)
 {
     std::cout<< "Leaf Cannot Remove Childrren" << std::endl;
 }

 std::shared_ptr<Graphic> Graphic::GetChild(int iIndex)
 {
     std::cout<< "Leaf Cannot Get Child" << std::endl;
     return nullptr;
 }

 std::shared_ptr<Graphic>& Graphic::GetParent()
 {
     return m_pParent;
 }

 void Graphic::Opereate()
 {
     std::cout<< "Default Operate Executed" <<std::endl;
 }

 Circle::Circle(std::shared_ptr<Graphic> pParent)
     :Graphic(pParent)
 {
 }

 void Circle::Opereate()
 {
     std::cout << "Circle Operate Exeeuted" << std::endl;
 }

 Retangle::Retangle(std::shared_ptr<Graphic> pParent)
     :Graphic(pParent)
 {
 }

 void Retangle::Opereate()
 {
     std::cout << "Retangle Operate Exeeuted" << std::endl;
 }


  Line::Line(std::shared_ptr<Graphic> pParent)
     :Graphic(pParent)
 {
 }

 void Line::Opereate()
 {
     std::cout << "Line Operate Exeeuted" << std::endl;
 }


 Picture::Picture(std::shared_ptr<Graphic> pParent) 
    : Graphic(pParent)
{
}

 void Picture::Add(std::shared_ptr<Graphic>& pGraphic)
 {
     m_vecChildrenGraphics.push_back(pGraphic);

     std::cout<< "ChildrenGraphics Add Success" <<std::endl;
 }

 void Picture::Remove(std::shared_ptr<Graphic>& pGraphic)
 {
     for(auto iter=m_vecChildrenGraphics.begin();
         iter!=m_vecChildrenGraphics.end();++iter)
     {
         if((*iter) == pGraphic)
         {
             m_vecChildrenGraphics.erase(iter);
             std::cout<< "ChildrenGraphics remove Success"<<std::endl;
             break;
         }
     }
 }

 std::shared_ptr<Graphic> Picture::GetChild(int iIndex)
 {
     auto count = m_vecChildrenGraphics.size();
     if(iIndex <= count -1)
     {
         std::cout<< "Get Child Success" <<std::endl;
         return m_vecChildrenGraphics[iIndex];
     }

     return nullptr;
 }

 void Picture::Opereate()
 {
     for(auto iter=m_vecChildrenGraphics.begin();
         iter!=m_vecChildrenGraphics.end();++iter)
     {
         (*iter)->Opereate();
     }
 }
#include "Graphic.h"
#include <iostream>

int main()
{
    std::shared_ptr<Graphic> pPicture(new Picture(nullptr));

    std::shared_ptr<Graphic> pCircle(new Circle(pPicture));

    std::shared_ptr<Graphic> pRetangle(new Retangle(pPicture));

    std::shared_ptr<Graphic> pLine(new Line(pPicture));

    pPicture->Add(pCircle);
    pPicture->Add(pRetangle);
    pPicture->Add(pLine);

    pPicture->Opereate();

    if(nullptr == pPicture->GetParent())
    {
        std::cout<< "pPicture is root" <<std::endl;
    }

    auto pNode = pPicture->GetChild(0);

    auto pParent = pNode->GetParent();

    if(nullptr != pParent)
    {
        std::cout<<"Node has Parent"<<std::endl;
    }

    std::cout << "Parent Operate:"<<std::endl;

    pParent->Opereate();

    pNode->Opereate();

    pNode->Add(pNode);

    pNode->Remove(pNode);

    pNode->GetChild(0);


    pNode = pPicture->GetChild(1);

    if(nullptr != pNode->GetParent())
    {
        std::cout<<"Node has Parent"<<std::endl;
    }

    pNode->Opereate();

    pNode->Add(pNode);

    pNode->Remove(pNode);

    pNode->GetChild(0);

     pNode = pPicture->GetChild(2);

    if(nullptr != pNode->GetParent())
    {
        std::cout<<"Node has Parent"<<std::endl;
    }

    pNode->Opereate();

    pNode->Add(pNode);

    pNode->Remove(pNode);

    pNode->GetChild(0);

    pPicture->Remove(pCircle);
    pPicture->Remove(pRetangle);
    pPicture->Remove(pLine);


    while(1);

    



}

6.测试结果 

    Composite(组合)--对象结构型模式_第1张图片

7.效果

  • 定义了包含基本对象和组合对象的类层次结构 基本对象可以被组合成更复杂的组合对象,而这个组合对象又可以被组合。
  • 简化客户代码 客户可以一致地使用组合结构和单个对象。
  • 使得容易增加新类型的组件 新定义的Composite或Leaf子类自动地与已有的结构和客户代码一起工作,客户程序不需因新的Component类而改变。
  • 使设计变的更加一般化。

你可能感兴趣的:(Composite(组合)--对象结构型模式)