Iterator Design Pattern
The Iterator design pattern states the elements of a collection (an aggregate object) should be accessed and traversed without exposing its representation (underlying data structure).
This is achieved by defining an Iterator object to traverse a data structure and access its elements.
Iterator pattern is widely used in Java Collection Framework (the data structures built into Java). To implement the Iterator pattern, Java API provides two interfaces: Iterable
and Iterator
.
The built-in array is special!
The built-in array is an exception in the sense that it is an iterable (thus can be used with the enhanced for loop) but does not implement the Iterable
interface. The built-in array is a special construct, a cross between primitive and objects!
Iterator pattern hides the actual implementation of traversal through a data structure from the clients of it. We will apply this pattern to the design of our data structures in this course, starting with the IndexedList
ADT.
Resources
Refactoring Guru has a great article on the Iterator pattern.