1 Star 0 Fork 621

旧光影里的少年/DesignPattern

forked from Micooz/DesignPattern 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
CompositePattern.cpp 1.07 KB
一键复制 编辑 原始数据 按行查看 历史
Micooz 提交于 2014-10-04 12:21 +08:00 . 加入组合模式,装饰模式以及享元模式
#include <iostream>
#include <vector>
using namespace std;
class Component {
public:
virtual void Operation() { }
virtual void Add(const Component& com) { }
virtual void Remove(const Component& com) { }
virtual Component* GetChild(int index) {
return 0;
}
virtual ~Component() { }
};
class Composite :public Component {
public:
void Add(Component* com) {
_coms.push_back(com);
}
void Operation() {
for (auto com : _coms)
com->Operation();
}
void Remove(Component* com) {
//_coms.erase(&com);
}
Component* GetChild(int index) {
return _coms[index];
}
private:
std::vector<Component*> _coms;
};
class Leaf :public Component {
public:
void Operation() {
cout << "Leaf::Operation..." << endl;
}
};
int main() {
Leaf *leaf = new Leaf();
leaf->Operation();
Composite *com = new Composite();
com->Add(leaf);
com->Operation();
Component *leaf_ = com->GetChild(0);
leaf_->Operation();
delete leaf;
delete com;
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/yanjun_coder/DesignPattern.git
git@gitee.com:yanjun_coder/DesignPattern.git
yanjun_coder
DesignPattern
DesignPattern
master

搜索帮助