Applied Design Patterns with Java

Behavioral :: Iterator (257) {C ch 19}


Collaborations

Consequences

Iterator has the following benefits and liabilities:

  1. 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.
  2. Iterators simplify the Aggregate interface. Iterator's traversal interface eliminates the need for a similar interface in Aggregate, simplifying the aggregate's interface.
  3. 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