Composite模式
2015-07-14 09:50
337 查看
在开发时,如果遇到使用递归构建树状的组合结构,那么可以考虑使用Composite模式。Composite模式将对象组合成树形结构,来表示部分、整体的层次结构。
其类结构如图所示:
在Component中声明了所有管理子类对象的方法,因此实现Component接口的子类都有了Add、Remove等功能,这样叶子节点和枝节点对于外界没有区别;但是因为Leaf类本身不具备Add、Remove等功能,实现也没有意义。
在实现时,管理叶子节点需要容器,这里使用了vector。
//Component.h
//Component.cpp
//Composite.h
//Composite.cpp
//Leaf.h
//Leaf.cpp
//main.cpp
其类结构如图所示:
在Component中声明了所有管理子类对象的方法,因此实现Component接口的子类都有了Add、Remove等功能,这样叶子节点和枝节点对于外界没有区别;但是因为Leaf类本身不具备Add、Remove等功能,实现也没有意义。
在实现时,管理叶子节点需要容器,这里使用了vector。
//Component.h
//Component.h #ifndef _COMPONENT_H_ #define _COMPONENT_H_ class Component { public: Component(); virtual ~Component(); virtual void Operation() = 0; virtual void Add(const Component&); virtual void Remove(const Component&); virtual Component* GetChild(int); }; #endif
//Component.cpp
//Component.cpp #include"Component.h" Component::Component(){} Component::~Component(){} void Component::Add(const Component& com) {} void Component::Remove(const Component& com) {} Component* Component::GetChild(int index) { return 0; }
//Composite.h
//Composite.h #ifndef _COMPOSITE_H_ #define _COMPOSITE_H_ #include"Component.h" #include<vector> class Composite :public Component { public: Composite(); ~Composite(); void Operation(); void Add(Component* com); void Remove(Component* com); Component* GetChild(int index); private: std::vector<Component*> comVec; }; #endif
//Composite.cpp
//Composite.cpp #include"Component.h" #include"Composite.h" Composite::Composite() { } Composite::~Composite() { } void Composite::Operation() { std::vector<Component*>::iterator comIter = comVec.begin(); for (; comIter != comVec.end(); comIter++) { (*comIter)->Operation(); } } void Composite::Add(Component* com) { comVec.push_back(com); } void Composite::Remove(Component* com) { std::vector<Component*>::iterator comIter = comVec.begin(); for (; comIter != comVec.end(); comIter++) { if (*comIter == com) { comVec.erase(comIter); return; } } } Component* Composite::GetChild(int index) { return comVec[index]; }
//Leaf.h
//Leaf.h #ifndef _LEAF_H_ #define _LEAF_H_ #include"Component.h" class Leaf :public Component { public: Leaf(); ~Leaf(); void Operation(); }; #endif
//Leaf.cpp
//Leaf.cpp #include"Leaf.h" #include<iostream> Leaf::Leaf() {} Leaf::~Leaf() {} void Leaf::Operation() { std::cout << "Leaf operation..." << std::endl; }
//main.cpp
//main.cpp #include"Component.h" #include"Composite.h" #include"Leaf.h" int main() { Leaf* l = new Leaf(); l->Operation(); Composite *com = new Composite(); com->Add(l); com->Operation(); Component* ll = com->GetChild(0); ll->Operation(); return 0; }
相关文章推荐
- jQuery 常用的代码片段
- 黑马程序员--多线程
- ANDROID AIDL(安桌接口定义语言)
- C++使用libcurl做HttpClient
- Android中的线程与进程之间的关系简单解释
- UIKeyboard键盘相关知识点-IOS开发
- Asp.net mvc4 发布你的程序到IIS7.0
- HDU 1597 find the nth digit
- AudioSession/AudioCaptureSession的分析与使用
- Atitit.web三编程模型 Web Page Web Forms 和 MVC
- 使用RESTClient插件进行数据模拟(GET,POST)提交
- Spring taskExecutor运行后台线程在Tomcat停止时时主动退出的方法
- POJ 3438 Look and Say(水~)
- 最优化学习笔记(五)牛顿法及拟牛顿法
- 网络请求
- Python实现批量将word转html并将html内容发布至网站的方法
- Ubuntu 14 安装单机版storm 0.9.5
- ES6详解一:复习ES5 新增内容
- openwrt ubus 移植到ARM上
- C++ 使用delete删除指针(1