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

2.Composite 模式结构图

Composite Pattern

3.实现

 1 #ifndef _COMPONENT_H_ 
 2 #define _COMPONENT_H_
 3 
 4 class Component 
 5 { 
 6 public: 
 7     Component();
 8     virtual ~Component();
 9 public: 
10     virtual void Operation() = 0;
11     virtual void Add(const Component& );
12     virtual void Remove(const Component& );
13     virtual Component* GetChild(int );
14 protected:
15 private:
16 };
17 
18 #endif
Component.h

相关文章: