-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathComposite.cpp
76 lines (60 loc) · 1.38 KB
/
Composite.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
class Compenent {
public:
std::string id;
std::list<Compenent *> compList;
Compenent(std::string s) {
this->id = s;
}
virtual void operation() {};
void Add(Compenent *c) {
compList.push_back(c);
};
void Remove(Compenent *c) {
compList.remove(c);
};
virtual void GetChild() {};
};
class Leaf : public Compenent {
public:
Leaf(std::string s) : Compenent(s) {};
void operation() override {
std::cout << "Leaf" << std::endl;
}
void GetChild() override {
std::cout << this->id << std::endl;
}
};
class Composite : public Compenent {
public:
Composite(std::string s) : Compenent(s) {};
void operation() override {
std::cout << "Composite" << std::endl;
}
void GetChild(){
for (auto c : this->compList){
std::cout << this->id << std::endl;
c->GetChild();
}
}
};
//int main() {
// Compenent *c = new Composite("c");
// Compenent *c1 = new Composite("c1");
// Compenent *c2 = new Composite("c2");
// Compenent *l = new Leaf("l");
// Compenent *l1 = new Leaf("l1");
// Compenent *l2 = new Leaf("l2");
//
// c->Add(c1);
// c->Add(c2);
// c->Add(l);
// c2->Add(l2);
// c1->Add(l1);
//
// c->GetChild();
// return 0;
//};