-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomposite.cpp
More file actions
114 lines (94 loc) · 2.78 KB
/
composite.cpp
File metadata and controls
114 lines (94 loc) · 2.78 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*!
* @brief : This example was originally declared in "Desgin Patterns :
* Elements of reusable Object-oriented software".
*/
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
// ------- COMPONENT ------- //
/*!
* @brief Component - declares the common interface for every
* objects (Leaf or Composite) of the composition.
*/
class Graphic {
public:
virtual ~Graphic() = default;
virtual void draw (void) const = 0; // An operation method
virtual bool isComposite(void) const { return false; }
virtual void setParent(Graphic* p_parent) { m_parent = p_parent; }
virtual Graphic* getParent(void) const { return m_parent; }
/*!
* Note : These are child-management operations.
* Defining them in the Component class ensure you that you
* will not need to expose any Leaf/Composite to the client code.
*/
virtual void remove (Graphic *g) { /* EMPTY */ }
virtual void add (Graphic *g) { /* EMPTY */ }
protected:
Graphic* m_parent;
};
// --------- LEAFS --------- //
/*!
* @brief Leafs - Leaf objects of the composition.
* - They have no children.
* - They define the behaviour of the primitive objects.
*/
class Line : public Graphic {
public:
void draw(void) const override { std::cout << "Line\n"; }
};
class Rectangle : public Graphic {
public:
void draw(void) const override { std::cout << "Rectangle\n"; }
};
class Text : public Graphic {
public:
void draw(void) const override { std::cout << "Text\n"; }
};
// --------- COMPOSITE --------- //
/*!
* @brief Composite - Composite objects
* - Stores children (Leaf or Composite)
* - Implement the Component interface for
* the child-related operations
*/
class Picture : public Graphic {
public:
Picture(std::string&& p_name = ""): m_name(p_name) {}
virtual ~Picture() = default;
virtual bool isComposite(void) const override { return true; }
void draw(void) const override {
std::for_each( std::begin(m_sons),
std::end (m_sons),
[](Graphic* it){ it->draw(); } );
}
void add (Graphic* p_elem) { m_sons.push_back(p_elem); }
void remove(Graphic* p_elem) {
m_sons.erase(std::remove(std::begin(m_sons),
std::end (m_sons),
p_elem), std::end (m_sons));
}
private:
std::vector<Graphic*> m_sons;
std::string m_name;
};
// --------- CLIENT CODE --------- //
/*!
* @brief : Client Code
* Can work with Picture just as if
* it was a Leaf.
*/
int main()
{
Line line; line.draw();
Rectangle rect; rect.draw();
Text text; text.draw();
std::cout << std::endl;
Picture pic;
pic.add(&line);
pic.add(&rect);
pic.add(&text);
pic.draw();
return 0;
}