-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcomposite.cpp
70 lines (61 loc) · 1.19 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
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
class Graphic {
public:
virtual void draw() const = 0;
virtual void remove(Graphic *g) {}
virtual void add(Graphic *g) {}
virtual void getChild(int) {}
virtual ~Graphic() {}
};
class Line : public Graphic {
public:
void draw() const {
cout << "Line draw()\n";
}
};
class Rectangle : public Graphic {
public:
void draw() const {
cout << "Rectangle draw() \n";
}
};
class Text : public Graphic {
public:
void draw() const {
cout << "Text draw() \n";
}
};
// Composite
class Picture : public Graphic {
public:
void draw() const {
// for each element in gList, call the draw member function
for_each(gList.begin(), gList.end(), mem_fun(&Graphic::draw));
}
void add(Graphic *aGraphic) {
gList.push_back(aGraphic);
}
private:
vector<Graphic*> gList;
};
int main()
{
Line line;
line.draw();
Rectangle rect;
rect.draw();
Text text;
text.draw();
cout<<"Composite:"<<endl;
Picture pic;
pic.add(&line);
pic.add(&rect);
pic.add(&text);
pic.add(&rect);
pic.draw();
return 0;
}