Those who have written any C++ may know the iterator idiom from the STL. The
Iterator pattern is not quite the same. The motivation is the same, but the
implementation is quite different. An Iterator allows the user to control the
progress through an aggregate without opening up access to the internals but
also allows for multiple concurrent traversals.
To create an Iterator, we ask the object to CreateIterator, which we store
to use later. We can later call First to reset the Iterator, call IsDone
to find out if we’ve reached the end of the container, and CurrentItem and
Next to use the item at and step past the current position in the container.
In C++, the iterator is even simpler in structure as it does not necessarily
include a pointer to the container. There is no reset, no done, only
current, advance, and the capacity to compare with others of the same type.
The Iterator pattern is a simple object to understand so it can potentially
keep systems more tractable for a maintenance programmer. For C++, there is
little reason to use them as the STL iterator idiom is very well known and
provides a more effective solution in almost all cases.
An Iterator is another pattern of nominalisation. In this case, the object
represents progress through a specific container. It abstracts the status of a
loop, relinquishing control to the user rather than requiring callbacks as the
container loops over its elements. It’s another inversion of control, but the
other way around to usual.