The Iterator design pattern provides a solution:
Encapsulate the access and traversal of an aggregate
in a separate Iterator object.
Clients request an
Iterator object from an aggregate
(createIterator())
and use it to access and traverse the aggregate.
Describing the Iterator design in more detail is the
theme of the following sections.
See Applicability section for all problems Iterator can solve.
Iterator
objects:
Iterator | next(),hasNext()).
Iterator1,…)
that implement the Iterator interface.
previous(),hasPrevious().
createIterator()).
Iterator objects
to access and traverse an aggregate object
in different ways.
Multiple traversals can be in progress
on the same aggregate object (simultaneous traversals).
Background Information
next(),hasNext(),remove())
next(),hasNext(),previous(),hasPrevious(),remove(),… ).
iterator() and listIterator()).