Ir para o conteúdo principal

← todas as notas

Iterator Pattern

“Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.”

The Iterator pattern (GoF, 1994) decouples traversal logic from the collection itself. The collection provides an iterator object; the client uses next() and hasNext() without knowing whether the underlying structure is an array, a tree, a hash map, or something else.

This pattern is so fundamental that most modern languages have built it into the language: for...of in JavaScript, for...in in Python, foreach in C#, Iterator trait in Rust. You use it every day, even if you don’t call it by name.