Applied Design Patterns with Java
Behavioral :: Iterator (257) {C ch 19}
Collaborations
- A ConcreteIterator keeps track of the current object in
the aggregate and can compute the succeeding object in the traversal.
Consequences
Iterator
has the following benefits and liabilities:
- It supports variations in the traversal of an aggregate.
Complex aggregates may be traversed in many ways. Code generation
and semantic checking involve traversing parse trees. Code generation may traverse the parse tree inorder or preorder. Iterators make
it easy to change the traversal algorithm: Just replace the Iterator instance with a different one. It's easy to define Iterator subclasses to support new traversals.
- Iterators simplify the Aggregate interface. Iterator's traversal
interface eliminates the need for a similar interface in Aggregate, simplifying the aggregate's interface.
- More than one traversal can be pending on an aggregate.
An Iterator keeps track of its own traversal state. So more than one traversal can be in
progress at one time.
Catalog Behavioral Prev Next