Composite 组合对象结构型模式

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

      Composite 模式适用于:1)你想表示对象的“部分-整体”层次结构。2)你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。

Composite 的通用结构图如下:

Composite

典型的 Composite 对象结构如下图所示:

CompositeObject

注:1)客户通过 Component 接口操纵部件的对象。

     2)Component: 为组合中的对象声明接口;在适当的情况下,实现所有类共有接口的缺省行为;声明一个接口用于访问和管理 Component 的子组件。在递归结构中定义一个接口,用于访问一个父部件,并在合适的情况下实现它。

     3)Leaf:在组合中表示叶节点对象,叶节点没有子节点;在组合中定义对象行为。

     4):Composite:定义有子部件的那些部件的行为;存储子部件;在 Composite 接口中实现与子部件有关的操作。

        其中:Add 和 Remove 操作用于管理子部件。1):如果强调透明性,则应该在类层次结构的根部Component 定义子节点管理接口 ,这样可以一致地使用所有的组件,但这一方法是以安全性为代价的,因为客户有可能会做一些无意义的事情,例如在 Leaf 中增加或删除对象等。2):如果强调安全性,则在 Composite 类中定义管理子部件的方法,但这样损失了透明性,因为 Leaf 和 Composite 具有不同的接口。GOF 的书中指出,这一模式应该强调透明性。

Composite 组合对象结构型模式代码示例如下:

   1:  //Composite.h
   2:  #pragma once
   3:   
   4:  #include 
   5:  #include 
   6:   
   7:  // 组合中的抽象基类
   8:  class Component{
   9:  public:
  10:      Component(){}
  11:      virtual ~Component(){    }
  12:   
  13:      // 纯虚函数, 只提供接口, 没有默认的实现
  14:      virtual void Operation() = 0;
  15:   
  16:      // 强调透明性,在层次结构的根部定义 Add 和 Remove 用于管理子部件
  17:      // 虚函数, 提供接口, 有默认的实现但是什么都不做
  18:      virtual void Add(Component* pChild);        //添加一个子组件
  19:      virtual void Remove(Component* pChild);        //删除一个子组件
  20:      virtual Component* GetChild(int nIndex);    //获得子组件的指针
  21:   
  22:      // 当该结构中子类数目相对较少时,可以在基类中存放子类指针
  23:      // private:
  24:      // std::list m_ListOfComponent;
  25:  };
  26:   
  27:  // 派生自 Component, 是其中的叶子组件的基类
  28:  // Leaf1, for test
  29:  class Leaf1 : public Component{
  30:  public:
  31:      Leaf1(){}
  32:      virtual ~Leaf1(){    }
  33:   
  34:      virtual void Operation();
  35:  };
  36:  // Leaf2, for test
  37:  class Leaf2 : public Component{
  38:  public:
  39:      Leaf2(){}
  40:      virtual ~Leaf2(){    }
  41:   
  42:      virtual void Operation();
  43:  };
  44:   
  45:  //
  46:  // Composite 派生自 Component, 是其中的含有子件的组件的基类
  47:  class Composite : public Component{
  48:  public:
  49:      Composite(){    }
  50:      virtual ~Composite();
  51:   
  52:      // 对所有 m_ListOfComponent,调用其 Operation()
  53:      virtual void Operation();
  54:   
  55:      virtual void Add(Component* pChild);
  56:      virtual void Remove(Component* pChild);
  57:      virtual Component* GetChild(int nIndex);
  58:   
  59:  private:
  60:      // Composite 可以使用多种数据结构存贮它们的子节点
  61:      // 包括连接列表、树、数组和 hash 表。数据结构的选择取决于效率。
  62:      // 没有必要使用通用的数据结构,有时对于每个子节点, Composite 
  63:      // 都有一个变量与之对应,这就要求 Composite 的每个子类都要实现自己的管理接口。
  64:   
  65:      // 这里采用 list 容器去保存子组件
  66:      std::list m_ListOfComponent;
  67:  };
  68:   
  69:  // Derived from Composite, for test
  70:  class Leaf3 : public Composite{
  71:  public:
  72:      Leaf3(){}
  73:      virtual ~Leaf3(){    }
  74:   
  75:      virtual void Operation();
  76:  };
  77:  // Derived from Composite, for test
  78:  class Leaf4 : public Composite{
  79:  public:
  80:      Leaf4(){}
  81:      virtual ~Leaf4(){    }
  82:   
  83:      virtual void Operation();
  84:  };
 
   1:  // Composite.cpp
   2:  #include "Composite.h"
   3:  #include 
   4:  #include 
   5:   
   6:  // Component成员函数的实现
   7:  void Component::Add(Component* pChild){}
   8:  void Component::Remove(Component* pChild){}
   9:  Component* Component::GetChild(int nIndex){ return NULL;}
  10:   
  11:  // Leaf1 成员函数的实现, for test
  12:  void Leaf1::Operation(){
  13:      std::cout << "Operation by leaf1" << std::endl;
  14:  }
  15:  // Leaf2 成员函数的实现, for test
  16:  void Leaf2::Operation(){
  17:      std::cout << "Operation by leaf2" << std::endl;
  18:  }
  19:   
  20:  // Composite 成员函数的实现
  21:  Composite::~Composite(){
  22:      std::list::iterator iter1, iter2, temp;
  23:   
  24:      for (iter1  = m_ListOfComponent.begin(), 
  25:          iter2 = m_ListOfComponent.end(); iter1 != iter2;)
  26:      {
  27:          temp = iter1;
  28:          ++iter1;
  29:          delete (*temp);
  30:      }    
  31:      m_ListOfComponent.clear();
  32:  }    
  33:   
  34:  // Composite::Operation 对所有的
  35:  void Composite::Operation(){
  36:      //std::cout << "Operation by Composite" << std::endl;
  37:      std::list::iterator iter1, iter2;
  38:      for (iter1  = m_ListOfComponent.begin(),
  39:          iter2 = m_ListOfComponent.end();iter1 != iter2;++iter1)
  40:      {
  41:          (*iter1)->Operation();
  42:      }
  43:  }
  44:  // Add, Remove 管理子部件
  45:  void Composite::Add(Component* pChild){
  46:      m_ListOfComponent.push_back(pChild);
  47:  }
  48:   
  49:  void Composite::Remove(Component* pChild){
  50:      std::list::iterator iter;
  51:   
  52:      iter = find(m_ListOfComponent.begin(), m_ListOfComponent.end(), pChild);
  53:      if (m_ListOfComponent.end() != iter){
  54:          m_ListOfComponent.erase(iter);
  55:      }
  56:  }
  57:   
  58:  Component* Composite::GetChild(int nIndex){
  59:      if (nIndex <= 0 || nIndex > m_ListOfComponent.size())
  60:          return NULL;
  61:   
  62:      std::list::iterator iter1, iter2;
  63:      int i;
  64:      for (i = 1, iter1  = m_ListOfComponent.begin(), 
  65:          iter2 = m_ListOfComponent.end();iter1 != iter2;++iter1, ++i){
  66:          if (i == nIndex)
  67:              break;
  68:      }
  69:   
  70:      return *iter1;
  71:  }
  72:   
  73:  // Leaf3 成员函数的实现, for test
  74:  void Leaf3::Operation(){
  75:      std::cout << "Operation by leaf3" << std::endl;
  76:  }
  77:  // Leaf4 成员函数的实现, for test
  78:  void Leaf4::Operation(){
  79:      std::cout << "Operation by leaf4" << std::endl;
  80:  }
//test
   1:  //test.cpp
   2:  #include "Composite.h"
   3:  #include 
   4:   
   5:  int main()
   6:  {
   7:      Component* pComponent = new Composite;
   8:      Leaf1 *pLeaf1 = new Leaf1();
   9:      Leaf2 *pLeaf2 = new Leaf2();
  10:   
  11:      Leaf3 *pLeaf3 = new Leaf3();
  12:      Leaf4 *pLeaf4 = new Leaf4();
  13:   
  14:      pComponent->Add(pLeaf1);
  15:      pComponent->Add(pLeaf2);
  16:      pComponent->Add(pLeaf3);
  17:      pComponent->Add(pLeaf4);
  18:   
  19:      pComponent->Operation();
  20:   
  21:      delete pComponent;
  22:   
  23:      return EXIT_SUCCESS;
  24:  }
 
 
发布了54 篇原创文章 · 获赞 3 · 访问量 12万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章