-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposite.h
More file actions
75 lines (70 loc) · 1.2 KB
/
composite.h
File metadata and controls
75 lines (70 loc) · 1.2 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
#pragma once
#include <iostream>
#include <algorithm>
#include <list>
#include <string>
using namespace std;
class component
{
protected:
component* _parent;
public:
void set_parent(component* parent)
{
this->_parent = parent;
}
component* get_parent() const
{
return this->_parent;
}
virtual void add(component* _component) {}
virtual void remove(component* _component) {}
virtual bool is_composite() const
{
return false;
}
virtual string Operation() const = 0;
};
class leaf : public component
{
public:
string Operation() const override
{
return "LEAF";
}
};
class composite : public component
{
protected:
list<component*> _children;
public:
void add(component* _component) override
{
_children.push_back(_component);
_component->set_parent(this);
}
void remove(component* _component) override
{
_children.remove(_component);
_component->set_parent(nullptr);
}
bool is_composite() const override
{
return true;
}
string Operation() const override
{
string result;
for (const component* c : _children)
{
if (c == _children.back())
{
result += c->Operation();
}
else {
result += c->Operation() + "+";
}
}
return "branch(" + result + ");";
}
};