-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path12.source.cpp
More file actions
144 lines (122 loc) · 2.84 KB
/
12.source.cpp
File metadata and controls
144 lines (122 loc) · 2.84 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <string>
#include <vector>
const unsigned MAX_LEN = 80;
class DisplayBehaviour
{
public:
virtual void displayText(std::string const& s) = 0;
};
class RightBehaviour : public DisplayBehaviour
{
public:
void displayText(std::string const& content) override
{
unsigned numSpaces = MAX_LEN - content.size();
for (unsigned i = 0; i < numSpaces; i++) std::cout << " ";
std::cout << content;
}
};
class LeftBehaviour : public DisplayBehaviour
{
public:
void displayText(std::string const& content)
{
std::cout << content;
}
};
class CenterBehaviour : public DisplayBehaviour
{
public:
void displayiText(std::string const& content)
{
unsigned numSpaces = (MAX_LEN - content.size()) / 2;
for (unsigned i = 0; i < numSpaces; i++) std::cout << " ";
std::cout << content;
}
};
struct Entity {
virtual void display() = 0;
virtual void setDisplayBehaviour(DisplayBehaviour* db) = 0;
virtual ~Entity() { }
};
struct Text : Entity
{
std::string content;
// displayBehaviour is our strategy, remember Strategy pattern
DisplayBehaviour *displBehaviour{new LeftBehaviour()};
Text(std::string const& s) : content{s} { }
Text() {} // needed for the factory
void setDisplayBehaviour(DisplayBehaviour* db) override
{
if (displBehaviour)
{
// delete the old one so to
// avoid a memory leak
delete displBehaviour;
}
displBehaviour = db;
}
void display() override
{
displBehaviour->displayText(content);
std::cout << std::endl;
}
virtual ~Text()
{
delete displBehaviour;
}
};
struct Paragraph : Entity
{
std::vector<Entity*> lines;
Paragraph(std::vector<Entity*> const& ls) : lines{ls} {}
Paragraph() {} // needed for the factory
void display() override
{
for(Entity* line : lines)
{
line->display();
}
}
void setDisplayBehaviour(DisplayBehaviour* db) override
{
for(Entity* line : lines)
{
line->setDisplayBehaviour(db);
}
}
// TODO
};
enum EntityType {
text,
paragraph
};
class Factory {
public:
static Entity* create(EntityType type)
{
switch (type)
{
case text:
return new Text();
break;
case paragraph:
return new Paragraph();
break;
default:
return nullptr;
break;
}
}
};
int main()
{
std::string s = "some text";
Paragraph *p = dynamic_cast<Paragraph*>(Factory::create(paragraph));
p->lines = std::vector<Entity*>{new Text{"line 1"},
new Text{"line 2"}};
p->display();
p->setDisplayBehaviour(new RightBehaviour);
p->display();
}