Intent

Provide a way to move through a list of collection or aggregated objects without knowing its internal representations.

Where to use & benefits

Structure


Participants

Iterator  (java.util.Iterator)
defines an interface for accessing and traversing elements.

ConcreteIterator  (java.util.AbstaractList#iterator() (ArrayListIterator))
implements the Iterator interface and keeps track of the current position in the traversal of the aggregate.

Aggregate  (java.util.List)
defines an interface for creating an Iterator object

ConcreteAggregate  (java.util.ArrayList)
implements the Iterator creation interface to return an instance of the proper ConcreteIterator

Example

In the demonstration application, AppletPainter class uses Java ArrayList Iterator facility to iterate the Command objects in the CommandContainer class.

Class Diagram


Source Code Links