组合模式(结构型设计模式)
来源:互联网 发布:js 获取table tr td 编辑:程序博客网 时间:2024/06/05 23:18
结构图
单个对象和组合对象的使用具有一致性,将对象组合成树状结构以表示“整体—部分”
文件和文件夹的案例:
实现
class IFile{protected: string name;public: virtual void display() = 0; virtual ~IFile(){}};class File :public IFile{public: File(){ this->name = "未命名文件"; } File(string name){ this->name = name; } virtual void display(){ cout << name << endl; } ~File(){ cout << "del---File---" << name << endl; }};class Floder :public IFile{private: vector<IFile*> vec;public: Floder(){ this->name = "未命名文件夹"; } Floder(string name){ this->name = name; } virtual void display(){ cout << name<<endl; vector<IFile*>::iterator it; for (it = vec.begin(); it != vec.end(); it++){ (*it)->display(); } } void add(IFile *f){ vec.push_back(f); } ~Floder(){ vector<IFile*>::iterator it; for (it = vec.begin(); it != vec.end(); it++){ delete (*it); } cout << "del---Floder---" << name << endl; }};
Client
int main(void){ Floder *root = new Floder("root"); root->add(new File()); root->add(new File("aaa.txt")); Floder *dir = new Floder(); root->add(dir); dir->add(new File("bbb.txt")); dir->add(new File("bbbb.txt")); root->display(); delete root; system("pause"); return 0;}
调试结果:
阅读全文
0 0
- 组合模式(结构型设计模式)
- 设计模式 - 结构型模式 - 组合模式
- 设计模式-结构型模式-组合
- 设计模式--结构型--组合模式
- 设计模式8:组合模式【结构型】
- 设计模式-结构型03-组合模式
- 结构型设计模式---组合模式(Composite)
- 设计模式-结构型之组合模式
- 结构型设计模式-组合模式
- 结构型设计模式-组合模式
- 设计模式(七)组合模式Composite(结构型)
- 设计模式(七)组合模式Composite(结构型)
- 设计模式(七)组合模式Composite(结构型)
- 设计模式(九)组合模式(Composite)-结构型
- 设计模式(七)组合模式Composite(结构型)
- 设计模式(3)-结构型-组合模式(Composite)
- 【设计模式】—-(9)组合模式(结构型)
- 设计模式笔记(9)---组合模式(结构型)
- 1003. 我要通过!(20)
- 171025-用for语句画正方形【连续第二天】
- 欢迎使用CSDN-markdown编辑器
- Django编写RESTful API(三):基于类的视图
- 搜索引擎的基本工作原理
- 组合模式(结构型设计模式)
- 如何删除 Launchpad 中的无用图标
- python dlib学习(一):人脸检测
- 微信小程序API之网络(三)下载数据
- opencv 读取目标文件夹里图像并排序放入指定文件夹
- Java 四种访问控制符 public protected defualt private
- css让一个容器水平垂直居中
- GitHub万星项目:黑客成长技术清单
- 2017.10.24总结