Iterator pattern

An Iterator is a design pattern used in software engineering. Iterators are used with aggregates, or a collections. The iterator allows to access the elements of the aggregate. Sometimes the pattern is called cursor. Using the iterator, the elements of the aggregate can be accessed sequentially, without exposing the implementation.  Usually, iterators have methods to ask whether there is another item,  and to get ther next item. Depending on the implementation, they might also have methods for removing the current element, and for  searching the next element that meets certain conditions. Sometimes, the iterator can traverse the aggregate in one direction only, at other times, it can traverse it in both directions.

Iterators can be implemented in different ways:

 In the case of an external iterator, the client contorls iteration, with an internal iterator, the iterator controls the ideration. External iterators have been called active, internal iterators have been called passive.
 In some cases, the way the aggregate is traversed is dictated by the aggregate, in other cases, it is the iterator that controls the traversal
 Iterators must be robust against changes of the aggregate during iteration. Usually, iterators need to be registered with the aggregate. Creating an iterator becomes more ocmplex, as does changing the aggregate
 Sometims, iterators can use polymorphism; this comes at a cost though.
 Combining the iterator with the pattern of null object creates an empty (or null) iterator

Category:Design patterns