Behavioral design patterns are about how objects talk to each other and who does which job. They keep objects loosely coupled, which means one object does not depend much on the details of another.
The Iterator Pattern is a behavioral pattern. It lets you visit the items of a collection one at a time, without showing how the collection stores them.
1. What is the Iterator Pattern?
The Iterator Pattern gives the job of walking through a collection (traversal) to a separate object, called the iterator. You can then walk through an array, list, tree, or your own structure in the same way.
Real-Life Analogy: The Vending Machine
Think of a vending machine. You do not need to know how the snacks are arranged inside. You press a "Next" button to see the options one by one. The machine controls the order. It acts as the iterator and hides what is inside.
2. The Problem: Tight Coupling & Leaky Abstraction
In a simple design, a YouTubePlaylist might hand out its internal ArrayList directly.
- Breaks Encapsulation: Encapsulation means a class keeps its data private. Here, outside code can read or change the internal list.
- Tight Coupling: Outside code depends on the exact list type (e.g.,
Vector,List). If you change the type, that code breaks. - No Traversal Control: You cannot add "Shuffle Play" or "Reverse" in one place. You must edit every piece of outside code.
3. Class Diagram & Structural Breakdown
The diagram below shows the fixed design. The collection creates the iterator itself, so the client never touches the internal list.
4. How the Iterator Pattern Solves the Problem
| Problem | How Iterator Pattern Solves It |
|---|---|
| Direct structure access | The collection hides its internal data. You get elements one by one through the iterator. |
| No standard iteration | All traversal uses the same two methods (hasNext()/next()), so every collection works the same way. |
| Scattered logic | The iterator tracks the position (index). Client code stays short. |
| Hard to customize | Each way of walking (reverse, skip, shuffle, filter) can be a new iterator class. |
| Tight coupling | The client talks only to the iterator, not to the array or list behind it. |
5. Pros and Cons
Advantages
- Encapsulation: You can walk through a collection without knowing how it is built inside.
- Unified Interface: You use the same methods for every collection type.
- SRP & OCP Compliance: Walking through the data has its own class (Single Responsibility). You can add new iterators without editing old code (Open/Closed).
Disadvantages
- Boilerplate: Boilerplate means repeated setup code. A custom iterator needs extra classes and interfaces.
- Overkill: For a small, simple structure, a plain loop is enough.
- Manual Management: The client still has to write the loop itself, unless you hide it behind something else.
6. Real-World Examples
- Java Collection Framework: Classes like
ArrayListandHashSetimplement theIterableinterface. Itsiterator()method gives you anIterator. This is what thefor-eachloop uses. - Java Streams/Spliterator: Streams use a
Spliterator(Split + Iterator) inside to walk the elements. It can also split the data so several threads work on it at once.