2. Intent
• “Provide
a
way
to
access
the
elements
of
an
aggregate
object
sequen>ally
without
exposing
its
underlying
representa>on”
–
GoF
3. Mo3va3on
• Hide
implementa3on
details
• Allow
for
different
traversals
• Separate
traversal
code
from
the
list
itself
4. Basic
Structure
List Iterator
count() first()
add(Item) next()
remove(Item) isDone()
Item get(int index) Item currentItem()
• Provide
List
to
Iterator
(3ghtly
coupled)
• Iterator
keeps
a
Cursor
to
current
loca3on
7. Applicability
• List
processing
&
selec3on
• Tree
traversals.
eg.
depth
first
• Syntax
trees
-‐>
Interpreter?
8. Concurrency
• hasNext()
or
!isDone()
implies
subsequent
call
to
currentItem()
will
pass
• Iterators
get
a
copy
of
the
data?
• Iterators
keep
track
of
modifica3ons?