Java Reference
In-Depth Information
Additionally, Iterators allow clients to keep multiple navigation points to the same collection. You can think of an
Iterator as a cursor or pointer into the collection; with each call to the
Aggregate's
factory method, you can get
another pointer into the collection.
A drawback of iterators is that they give the illusion of order to unordered structures. For example, a set does not
support ordering, and its Iterator would likely provide the elements in an arbitrary sequence that could change
over time. If you don't realize this, you could write code that assumed consistency in the underlying structure,
which would result in problems later on.
Pattern Variants
The Iterator pattern has a number of implementation options.
A
ConcreteIterator
may be internal or external. External
Iterators
provide specific methods to the clients to
navigate within the collection, while internal Iterators cycle through the elements, performing some action
requested by the client. The external iterator is the more flexible option, but requires more coding on the client to
use the iterator.
ConcreteIterators
can be dynamic or static. A dynamic
ConcreteIterator
directly references its underlying
collection for elements, so is always guaranteed to reflect its state. A static
ConcreteIterator
, on the other hand,
creates a snapshot of the collection when it is created, and refers to the copy during client use.
Null iterators can be defined to make traversal of complex structures, such as trees, more straightforward. Using
an iterator that represents an “end node,” it is possible to write simple recursive code to visit all the nodes in the
tree.
Iterators can support a variety of different ways to move through a collection. This is particularly useful in
complex structures, such as the Composite pattern, where there might be a rationale to move through the elements
in a variety of different ways.
From a structural perspective, a
ConcreteIterator
can be defined as an inner class of the
ConcreteAggregate
,
or it can be defined in a separate class. The
ConcreteIterator
can hold the code to move through the collection,
or it might only represent the position within the collection.
Related Patterns
Related patterns include the following:
Factory Method (page 21) - Collection classes often define a factory method to produce the Iterator.
Visitor (page 121) - When the Visitor pattern is used on a group of objects, an Iterator is frequently used to
cycle through the elements.
Value List Handler [CJ2EEP] - The Value List Handler is based on the Iterator pattern in that it allows the client
to step through a collection.
Example
Note:
For a full working example of this code example, with additional supporting classes and/or a
RunPattern
class,
see “
Iterator
”
on page 389 of the “
Full Code Examples
” appendix.
This example uses the Java Collections Framework to provide iterating behavior for a pair of business aggregates.
The
java.util.Iterator
interface defines methods for the basic navigation methods required—
hasNext
and
next
. Note that the
Iterator
interface requires one-time-only traversal, since the only way to return to the
beginning is to get another
Iterator
from the collection.
The
Iterating
interface defines a single method,
getIterator
. This interface is used to identify any class in
the PIM that is capable of producing an Iterator for collection traversal.