23种设计模式C++实例之组合模式
2015-12-16 11:12
615 查看
为不同继承层次和组合层次的类,提供统一的接口。
#include <iostream>
#include <list>
/*
* 组合模式
* 让单个对象和组合对象具有一致的接口
* */
using namespace std;
class IFile {
public:
virtual ~IFile() {}
virtual void display() = 0;
virtual int add(IFile* iFile) = 0;
virtual int remove(IFile* iFile) = 0;
virtual list<IFile*>* getChild() = 0;
};
class File: public IFile {
public:
File(string name) {
m_name = name;
}
virtual void display() {
cout << m_name << endl;
}
virtual int add(IFile* iFile) {
return -1;
}
virtual int remove(IFile* iFile) {
return -1;
}
virtual list<IFile*>* getChild() {
return NULL;
}
private:
string m_name;
};
class Folder: public IFile {
public:
Folder(string name) {
m_name = name;
m_list = new list<IFile*>;
m_list->clear();
}
virtual ~Folder() {
if (m_list != NULL) {
delete m_list;
m_list = NULL;
}
}
virtual void display() {
cout << m_name << ":" << endl;
}
virtual int add(IFile* iFile) {
m_list->push_back(iFile);
return 0;
}
virtual int remove(IFile* iFile) {
m_list->remove(iFile);
return 0;
}
virtual list<IFile*>* getChild() {
return m_list;
}
private:
string m_name;
list<IFile*>* m_list;
};
// 递归显示树
void showTree(IFile* root, int level) {
if (root == NULL) {
return;
}
for(int i = 0; i < level; ++i) {
cout << "\t";
}
// 1 显示根节点
root->display();
list<IFile*>* mylist = root->getChild();
// 2 若子节点为文件,直接显示文件名
if (mylist == NULL) {
return;
} else {
// 3 若子节点为文件夹,则递归调用
for(list<IFile *>::iterator it = mylist->begin(); it != mylist->end(); ++it) {
if ((*it)->getChild() == NULL) {
for(int i = 0; i < level + 1; ++i) {
cout << "\t";
}
(*it)->display();
} else {
showTree(*it, level + 1);
}
}
}
}
int main() {
Folder* root = new Folder("C");
File* file1 = new File("file1.txt");
root->add(file1);
Folder* folder1 = new Folder("folder1");
File* file2 = new File("file2.txt");
folder1->add(file2);
root->add(folder1);
showTree(root, 0);
delete file1;
delete folder1;
delete root;
return 0;
}
#include <iostream>
#include <list>
/*
* 组合模式
* 让单个对象和组合对象具有一致的接口
* */
using namespace std;
class IFile {
public:
virtual ~IFile() {}
virtual void display() = 0;
virtual int add(IFile* iFile) = 0;
virtual int remove(IFile* iFile) = 0;
virtual list<IFile*>* getChild() = 0;
};
class File: public IFile {
public:
File(string name) {
m_name = name;
}
virtual void display() {
cout << m_name << endl;
}
virtual int add(IFile* iFile) {
return -1;
}
virtual int remove(IFile* iFile) {
return -1;
}
virtual list<IFile*>* getChild() {
return NULL;
}
private:
string m_name;
};
class Folder: public IFile {
public:
Folder(string name) {
m_name = name;
m_list = new list<IFile*>;
m_list->clear();
}
virtual ~Folder() {
if (m_list != NULL) {
delete m_list;
m_list = NULL;
}
}
virtual void display() {
cout << m_name << ":" << endl;
}
virtual int add(IFile* iFile) {
m_list->push_back(iFile);
return 0;
}
virtual int remove(IFile* iFile) {
m_list->remove(iFile);
return 0;
}
virtual list<IFile*>* getChild() {
return m_list;
}
private:
string m_name;
list<IFile*>* m_list;
};
// 递归显示树
void showTree(IFile* root, int level) {
if (root == NULL) {
return;
}
for(int i = 0; i < level; ++i) {
cout << "\t";
}
// 1 显示根节点
root->display();
list<IFile*>* mylist = root->getChild();
// 2 若子节点为文件,直接显示文件名
if (mylist == NULL) {
return;
} else {
// 3 若子节点为文件夹,则递归调用
for(list<IFile *>::iterator it = mylist->begin(); it != mylist->end(); ++it) {
if ((*it)->getChild() == NULL) {
for(int i = 0; i < level + 1; ++i) {
cout << "\t";
}
(*it)->display();
} else {
showTree(*it, level + 1);
}
}
}
}
int main() {
Folder* root = new Folder("C");
File* file1 = new File("file1.txt");
root->add(file1);
Folder* folder1 = new Folder("folder1");
File* file2 = new File("file2.txt");
folder1->add(file2);
root->add(folder1);
showTree(root, 0);
delete file1;
delete folder1;
delete root;
return 0;
}
相关文章推荐
- 23种设计模式C++实例之桥接模式
- 开发中遇到C++ 问题,记在这里,方便以后查看
- 23种设计模式C++实例之装饰模式
- 23种设计模式C++实例之代理模式
- c语言基础
- oc中new和init的区别
- 23种设计模式C++实例之适配器模式
- Blade - 腾讯开源的构建系统 c/c++编译环境
- c++11: allocator construct
- windows C++ 创建快捷方式API, 100% 能运行
- 在vs2008 C++下调试控制台程序的心得及技巧
- Pro*C/C++具体操作
- 23种设计模式C++实例之外观模式
- C语言中define的用法
- OC语言——把四个十进制三位数存放到一个数组中,然后按升序排序
- c++ 自定义排序容器set
- C++命名空间namespace
- C++ 类模板不能实现分离式编译
- Debug实模式调试
- c++ 线程类