Iterator
An Iterator provides the user with a way to access the elements of some kind of list sequentially, while keeping the elements of that list from exposing their underlying representation.
Introduction to Iterators
An iterator may be though of as a kind of pointer that has two basic operations, referencing one particular element in a collection, and pointing to the next element in the collection (current, and current.next). Depending on the language the iterator is implemented in, other functionality may be added to the iterator object, such as remove and update and so on.
Iterator ties together the object-oriented programming principles known as encapsulation and polymorphism. Using an iterator, you can manipulate the objects in a collection without explicitly knowing how the collection is implemented or what the collection is made up of (Different types of objects perhaps?). An iterator provides an interface to different iteration implementations, which contain the details of how to manipulate a specific collection, including which items in the collection to show (filtering) and in what order (sorting).
An easy way to think of iterators, is to also think of, Lists, Linked Lists, Binary Trees, and Hash Tables, because they operate very much in the same way that iterators do.
Implicit Iterators
Some object-oriented languages have iterator support included within the language, without having to implement an explicit iterator object. Some of these language include:
- C#
- Java (After 5.0)
- Python
- PERL
UML
Design class diagram in UML of the Iterator Pattern being used in a system.
Code Samples
The following are samples of code from C#, Java, Python, and PERL, displaying how they use their implicit iterators.
C#
// C#, implicit iteration foreach (Value v in list) Console.WriteLine(v);
Java
// Java, J2SE 5.0, implicit iteration for (Value v : list) System.out.print(v);
Python
# Python, implicit iteration for Value in List: print Value
PERL
# Perl, implicit iteration foreach $val (@list) { print "$val\n"; }
Examples
C++
This is a code snippet from a file called transfer.cc, created by the Gnome VFS Development Team. This particular function, transfer_list, transfers a list of URI's from the source list to the target list. The source code repository can be found here.
void transfer_list(const Glib::StringArrayHandle& source_uri_list, const Glib::StringArrayHandle& target_uri_list, TransferOptions options, ErrorMode error_mode, OverwriteMode overwrite_mode, const SlotProgress& slot) { typedef std::list< Glib::RefPtr<Uri> > uri_list; uri_list sources, targets; //Build lists of RefPtr<Uri>s from the strings: Glib::StringArrayHandle::const_iterator iter_target = target_uri_list.begin(); for(Glib::StringArrayHandle::const_iterator iter = source_uri_list.begin(); iter != source_uri_list.end(); ++iter) { if(iter_target != target_uri_list.end()) { sources.push_back( Uri::create(*iter) ); targets.push_back( Uri::create(*iter_target) ); iter_target++; } } transfer_list_uris(sources, targets, options, error_mode, overwrite_mode, slot); }