Iterator Design Pattern
Problem
When working with lists it is often nice not to have to know the internal details of how the list is implemented. We may have different ways to traverse the list and do not want to clutter each list class with all the different ways to traverse it. How can we do this?
Solution
An iterator is a class that is separate from a list class that it works on. The iterator knows how the list is implemented and knows how to traverse it in a specific way. The list class has methods on it that will instantiate a object of the appropriate iterator class for use in traverse itself. If you are familiar with STL(Standard Template Library), then you are familiar with the concept.
Consequences
An iterator keeps track of its own state of traversal in a list, and as such multiple iterators can be active in different places without having to implement support for this in the list class. As well, iterators can implement any kind of traversal; forward, backward, etc. An iterator is a very powerful tool for use with lists, you can use an iterator specify a position in a list for use with higher level manipulation routines, such as sorting.