Iterator patternIn object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled. For example, the hypothetical algorithm SearchForElement can be implemented generally using a specified type of iterator rather than implementing it as a container-specific algorithm. This allows SearchForElement to be used on any container that supports the required type of iterator. OverviewThe Iterator [1] design pattern is one of the 23 well-known "Gang of Four" design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse. What problems can the Iterator design pattern solve?
Defining access and traversal operations in the aggregate interface is inflexible because it commits the aggregate to particular access and traversal operations and makes it impossible to add new operations later without having to change the aggregate interface. What solution does the Iterator design pattern describe?
Different iterators can be used to access and traverse an aggregate in different ways.
See also the UML class and sequence diagram below. DefinitionThe essence of the Iterator Pattern is to "Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.".[3] StructureUML class and sequence diagram![]() In the above UML class diagram, the The UML sequence diagram
shows the run-time interactions: The UML class diagram![]() ExampleSome languages standardize syntax. C++ and Python are notable examples. C++C++ implements iterators with the semantics of pointers in that language. In C++, a class can overload all of the pointer operations, so an iterator can be implemented that acts more or less like a pointer, complete with dereference, increment, and decrement. This has the advantage that C++ algorithms such as This C++11 implementation is based on chapter "Generalizing vector yet again".[5] #include <iostream>
#include <stdexcept>
#include <initializer_list>
class Vector {
public:
using iterator = double*;
iterator begin() { return elem; }
iterator end() { return elem + sz; }
Vector(std::initializer_list<double> lst) :elem(nullptr), sz(0) {
sz = lst.size();
elem = new double[sz];
double* p = elem;
for (auto i = lst.begin(); i != lst.end(); ++i, ++p) {
*p = *i;
}
}
~Vector() { delete[] elem; }
int size() const { return sz; }
double& operator[](int n) {
if (n < 0 || n >= sz) throw std::out_of_range("Vector::operator[]");
return elem[n];
}
Vector(const Vector&) = delete; // rule of three
Vector& operator=(const Vector&) = delete;
private:
double* elem;
int sz;
};
int main() {
Vector v = {1.1*1.1, 2.2*2.2};
for (const auto& x : v) {
std::cout << x << '\n';
}
for (auto i = v.begin(); i != v.end(); ++i) {
std::cout << *i << '\n';
}
for (auto i = 0; i <= v.size(); ++i) {
std::cout << v[i] << '\n';
}
}
The program output is 1.21
4.84
1.21
4.84
1.21
4.84
terminate called after throwing an instance of 'std::out_of_range'
what(): Vector::operator[]
See also
References
External linksThe Wikibook Computer Science Design Patterns has a page on the topic of: Iterator implementations in various languages |
Portal di Ensiklopedia Dunia