The Iterator Pattern is a behavioral design pattern that lets you access elements of a collection without exposing its underlying representation (list, stack, tree, etc.).
This example demonstrates the Iterator pattern by creating a name repository with an iterator:
Container: Aggregate interface.Iterator: Iterator interface.NameRepository: Concrete aggregate with a private iterator implementation.
flowchart TD
A["App.java"] --> B["NameRepository"]
B --> C["Container (interface)"]
B --> D["Iterator (interface)"]
B --> E["NameIterator (private)"]
E --> D
NameRepository namesRepository = new NameRepository();
for(Iterator iter = namesRepository.getIterator(); iter.hasNext();){
String name = (String)iter.next();
System.out.println("Name : " + name);
}- When you want to access a collection's contents without exposing its internal structure.
- When you want to support multiple traversal methods for a collection.