Iterators

An iterator provides a common interface for traversing different containers. In C++ for example, all the container classes (lists, maps, vectors etc.) all have iterators that allow you to go through them. To get to the "next", iterators use the ++ operator. begin() returns first item in the container, end() allows you to determine if you have traversed through all items. The * operator allows you to get to the actual item. Note when the Design patterns textbook was written, these containers (lists, vectors etc.) was being added to the standard and not generally available.

If you are writing in a language that does not natively support generic containers, it is possible for you to define an iterator interface by following the pattern described in the textbook. If you are using a language that has built in iterator support, you should follow the conventions of that language in the creation of iterator for container classes that you write.

Quick Reference

Pattern Name Iterator
Other Names Cursor
Classification Object Behavioral
Intent Provides a common interface for iterating through containers

General Structure

Example

results matching ""

    No results matching ""