-
Notifications
You must be signed in to change notification settings - Fork 5
/
composite.cpp
60 lines (51 loc) · 1.58 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
// Composite design pattern
#include <iostream>
#include <memory>
#include <vector>
// basic elements interface
struct IShape {
virtual void add(std::shared_ptr<IShape> elem) = 0;
virtual void draw() const = 0;
};
// concrete basic element (leaf)
class Circle : public IShape {
// this is a leaf, nothing to add
void add(std::shared_ptr<IShape> elem) override {}
void draw() const override { std::cout << "Drawing a Circle\n"; }
};
// concrete basic element (leaf)
class Square : public IShape {
// this is a leaf, nothing to add
void add(std::shared_ptr<IShape>) override {}
void draw() const override { std::cout << "Drawing a Square\n"; }
};
// composite
class Composite : public IShape {
std::vector<std::shared_ptr<IShape>> collection_;
public:
void add(std::shared_ptr<IShape> elem) override {
collection_.push_back(elem);
}
void draw() const override {
std::cout << "Composite\n";
// delegate to the individual elements
for (auto&& elem : collection_) {
elem->draw();
}
}
};
int main() {
auto circle = std::make_shared<Circle>();
auto square = std::make_shared<Square>();
// create a first-level composition
auto shapes = std::make_shared<Composite>();
shapes->add(circle);
shapes->add(square);
shapes->draw();
// create a composition of 2 first level compositions
std::cout << '\n';
auto collection_of_shapes = std::make_shared<Composite>();
collection_of_shapes->add(shapes);
collection_of_shapes->add(shapes);
collection_of_shapes->draw();
}