-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConceptualExample02.cpp
More file actions
243 lines (196 loc) · 6.01 KB
/
ConceptualExample02.cpp
File metadata and controls
243 lines (196 loc) · 6.01 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// ===========================================================================
// ConceptualExample02.cpp // Iterator Pattern // Standard Variant
// ===========================================================================
#include <iostream>
#include <string>
#include <vector>
namespace IteratorPatternStandard {
template <typename T>
class IteratorBase
{
public:
virtual ~IteratorBase() {}
virtual void reset() = 0;
virtual const T& getCurrent() const = 0;
virtual bool hasNext() = 0;
};
// =======================================================================
template <typename T>
class AggregateBase
{
public:
virtual ~AggregateBase() {};
virtual IteratorBase<T>* createForwardIterator() = 0;
virtual IteratorBase<T>* createBackwardIterator() = 0;
};
// =======================================================================
template <typename T>
class ForwardIterator;
template <typename T>
class BackwardIterator;
template <typename T>
class ConcreteAggregate : public AggregateBase<T>
{
private:
std::vector<T> m_vector;
public:
IteratorBase<T>* createForwardIterator() {
return new ForwardIterator<T>{ this };
}
IteratorBase<T>* createBackwardIterator() {
return new BackwardIterator<T>{ this };
}
void add(const T& content)
{
m_vector.push_back(content);
}
int size() const
{
return static_cast<int> (m_vector.size());
}
T& get(int index)
{
return m_vector[index];
}
const T& get(int index) const
{
return m_vector[index];
}
};
// =======================================================================
template <typename T>
class ForwardIterator : public IteratorBase<T>
{
private:
const ConcreteAggregate<T>* m_aggregate;
int m_pos;
public:
ForwardIterator(const ConcreteAggregate<T>* agg)
: m_aggregate{ agg }, m_pos{ -1 }
{}
void reset() override
{
m_pos = -1;
}
const T& getCurrent() const override
{
return m_aggregate->get(m_pos);
}
bool hasNext() override
{
bool erg{ false };
if (m_pos < m_aggregate->size() - 1)
{
m_pos++;
erg = true;
}
return erg;
}
};
template <typename T>
class BackwardIterator : public IteratorBase<T>
{
private:
const ConcreteAggregate<T>* m_aggregate;
int m_pos;
public:
BackwardIterator(const ConcreteAggregate<T>* agg)
: m_aggregate{ agg }, m_pos{ agg->size() }
{}
void reset() override
{
m_pos = m_aggregate->size();
}
const T& getCurrent() const override
{
return m_aggregate->get(m_pos);
}
bool hasNext() override
{
bool erg{ false };
if (m_pos > 0)
{
m_pos--;
erg = true;
}
return erg;
}
};
// =======================================================================
}
void test_conceptual_example_02() {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
using namespace IteratorPatternStandard;
ConcreteAggregate<int> intContainer{};
for (int i = 0; i < 3; ++i) {
intContainer.add(i);
}
IteratorBase<int>* iter{ intContainer.createForwardIterator() };
while (iter->hasNext())
{
std::cout << iter->getCurrent() << std::endl;
}
delete iter;
std::cout << std::endl;
ConcreteAggregate<std::string> stringContainer{};
stringContainer.add("123");
stringContainer.add("456");
stringContainer.add("789");
std::cout << "Size: " << stringContainer.size() << std::endl;
std::cout << "[1]: " << stringContainer.get(1) << std::endl;
IteratorBase<std::string>* it{ stringContainer.createForwardIterator() };
while (it->hasNext())
{
std::cout << it->getCurrent() << std::endl;
}
delete it;
std::cout << std::endl;
it = stringContainer.createBackwardIterator();
while (it->hasNext())
{
std::cout << it->getCurrent() << std::endl;
}
std::cout << std::endl;
it->reset();
while (it->hasNext())
{
std::cout << it->getCurrent() << std::endl;
}
delete it;
}
void test_conceptual_example_03() {
using namespace IteratorPatternStandard;
ConcreteAggregate<int> intContainer{};
for (int i = 0; i < 10; ++i) {
intContainer.add(i);
}
IteratorBase<int>* intIter1{ intContainer.createForwardIterator() };
IteratorBase<int>* intIter2{ intContainer.createForwardIterator() };
// using first iterator three times
if (intIter1->hasNext()) {
std::cout << "First Iterator: " << intIter1->getCurrent() << std::endl;
}
if (intIter1->hasNext()) {
std::cout << "First Iterator: " << intIter1->getCurrent() << std::endl;
}
if (intIter1->hasNext()) {
std::cout << "First Iterator: " << intIter1->getCurrent() << std::endl;
}
std::cout << std::endl;
// now using second iterator three times
if (intIter2->hasNext()) {
std::cout << "Second Iterator: " << intIter2->getCurrent() << std::endl;
}
if (intIter2->hasNext()) {
std::cout << "Second Iterator: " << intIter2->getCurrent() << std::endl;
}
if (intIter2->hasNext()) {
std::cout << "Second Iterator: " << intIter2->getCurrent() << std::endl;
}
delete intIter1;
delete intIter2;
std::cout << std::endl;
}
// ===========================================================================
// End-of-File
// ===========================================================================