You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Iterator is a behavioral design pattern that lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).
2
+
// Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
3
+
// Appicability:
4
+
// (*) when your collection has a complex data structure under the hood, but you want to hide its complexity from clients (either for convenience or security reasons).
5
+
// (**) reduce duplication of the traversal code across your app.
6
+
// (***) when you want your code to be able to traverse different data structures or when types of these structures are unknown beforehand.
* Iterator interface: declares the operations required for traversing a collection: fetching the next element, retrieving the current position, restarting iteration, etc.
38
+
*/
39
+
template <typename T>
40
+
classIIterator
41
+
{
42
+
public:
43
+
virtualboolhasNext() const = 0;
44
+
virtualconst T *next() = 0;
45
+
virtual~IIterator() = default;
46
+
};
47
+
48
+
/**
49
+
* Aggregate interface: declares one or multiple methods for getting iterators compatible with the collection.
50
+
* Note that the return type of the methods must be declared as the iterator interface so that the concrete collections can return various kinds of iterators.
51
+
*/
52
+
template <typename T>
53
+
classIAggregate
54
+
{
55
+
public:
56
+
virtual IIterator<T> *createIterator() = 0;
57
+
virtual~IAggregate() = default;
58
+
};
59
+
60
+
/**
61
+
* Concrete Iterator: implement specific algorithms for traversing a collection.
62
+
* The iterator object should track the traversal progress on its own. This allows several iterators to traverse the same collection independently of each other.
0 commit comments