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

Composite模式也叫组合模式,是构造型的设计模式之一。通过递归手段来构造树形的对象结构,并可以通过一个对象来访问整个对象树。

 

Component (树形结构的节点抽象

- 为所有的对象定义统一的接口(公共属性,行为等的定义)

- 提供管理子节点对象的接口方法

- [可选]提供管理父节点对象的接口方法

 Leaf (树形结构的叶节点) Component的实现子类

 Composite(树形结构的枝节点) Component的实现子类

适用于:

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

  1 #include <iostream>
  2 using namespace std;
  3 #include "string"
  4 #include "list"
  5 //Component (树形结构的节点抽象)
  6 class IFile
  7 {
  8 public:
  9     virtual void display() = 0;
 10     virtual int add(IFile *ifile) = 0;
 11     virtual int remove(IFile *ifile) = 0;
 12     virtual list<IFile *>* getChild() = 0;
 13 };
 14 
 15 //文件结点   Leaf (树形结构的叶节点) Component的实现子类
 16 class File : public IFile
 17 {
 18 public:
 19     File(string name)
 20     {
 21         m_name = name;
 22     }
 23     virtual void display()
 24     {
 25         cout << m_name << endl;
 26     }
 27 
 28     virtual int add(IFile *ifile)
 29     {
 30         return -1;
 31     }
 32 
 33     virtual int remove(IFile *ifile)
 34     {
 35         return -1;
 36     }
 37 
 38     virtual list<IFile *>*  getChild()
 39     {
 40         return NULL;
 41     }
 42 private:
 43     string m_name;
 44 };
 45 
 46 //目录 结点  Composite(树形结构的枝节点) Component的实现子类
 47 class Dir : public IFile
 48 {
 49 public:
 50     Dir(string name)
 51     {
 52         m_name = name;
 53         m_list = new list<IFile *>;
 54         m_list->clear();
 55     }
 56     virtual void display()
 57     {
 58         cout << m_name << endl;
 59     }
 60 
 61     virtual int add(IFile *ifile)
 62     {
 63         m_list->push_back(ifile);
 64         return 0;
 65     }
 66 
 67     virtual int remove(IFile *ifile)
 68     {
 69         m_list->remove(ifile);
 70         return 0;
 71     }
 72 
 73     virtual list<IFile *>*  getChild()
 74     {
 75         return m_list;
 76     }
 77 private:
 78     string m_name;
 79     list<IFile *>  *m_list;
 80 };
 81 
 82 
 83 // 递归的显示树
 84 void showTree(IFile *root, int level)
 85 {
 86     int i = 0;
 87     if (root == NULL)
 88     {
 89         return ;
 90     }
 91     for (i=0; i<level; i++)
 92     {
 93         printf("	");
 94     }
 95     //1 显示根 结点
 96     root->display();
 97 
 98     //2  若根结点 有孩子 
 99         //判读孩子是文件,显示名字 )
100         //判断孩子是目录,showTree(子目录)
101 
102     list<IFile *>  *mylist = root->getChild();
103     if (mylist != NULL) //说明是一个目录
104     {
105         for (list<IFile *>::iterator it=mylist->begin(); it!=mylist->end(); it++)
106         {
107             if ( (*it)->getChild() == NULL )
108             {
109                 for (i=0; i<=level; i++) //注意 <= 
110                 {
111                     printf("	");
112                 }
113                 (*it)->display();
114             }
115             else
116             {
117                 showTree(*it, level+1);
118             }
119         }
120     }
121 }
122 
123 void main()
124 {
125     Dir *root = new Dir("C");
126     //root->display();
127 
128     Dir *dir1 = new Dir("111dir");
129     File *aaafile = new File("aaa.txt");
130 
131     //获取root结点下的 孩子集合
132     list<IFile *>  *mylist =  root->getChild();
133 
134     root->add(dir1);
135     root->add(aaafile);
136 
137     //  (111dir)   (aaa.txt)
138     //
139     for ( list<IFile *>::iterator it=mylist->begin(); it!=mylist->end(); it++ )
140     {
141         (*it)->display();
142     }
143 
144     //
145     Dir *dir222 = new Dir("222dir");
146     File *bbbfile = new File("bbb.txt");
147     dir1->add(dir222);
148     dir1->add(bbbfile);
149 
150     cout << "通过 showTree 方式 显示 root 结点下的 所有子结点" << endl;
151 
152     showTree(root, 0);
153 
154     cout<<"hello..."<<endl;
155     system("pause");
156     return ;
157 }

 优点:

原文地址:https://www.cnblogs.com/D-DZDD/p/7341316.html