forked from micooz/DesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompositePattern.cpp
More file actions
64 lines (49 loc) · 1.07 KB
/
Copy pathCompositePattern.cpp
File metadata and controls
64 lines (49 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#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;
}