GofPatterns Gofpatterns   Tweet
eWebProgrammer J2EEOnline

Behavioral Patterns  «Prev  Next»

Iterator Pattern

Iterator Pattern Description
The iterator pattern provides a consistent way to sequentially access items in a collection that is independent of and separate from the underlying collection. The figure below represents the iterator pattern.
Iterator Pattern
Iterator Pattern
Iterator Pattern in Java
Benefits of using the Iterator pattern:
  1. Supports variations in the traversal of a collection
  2. Simplifies the interface of the collection.

When to Use:
You should use the Interpreter pattern when you want to:
  1. Access a collection object’s contents without exposing its internal representation.
  2. Support multiple traversals of objects in a collection.
  3. Provide a uniform interface for traversing different structures in a collection.
In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and cannot be decoupled.
For example, the hypothetical algorithm SearchForElement can be implemented generally using a specified type of iterator rather than implementing it as a container-specific algorithm. This allows SearchForElement to be used on any container that supports the required type of iterator.