The Iterator pattern is a behavioral GoF design pattern by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides that provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. The traversal moves into a separate iterator object, which allows several traversals of the same aggregate and a uniform interface across different structures, such as depth-first and breadth-first walks of a tree.

GoF-Iterator Pattern

Details
Full Name

GoF Iterator Pattern (Behavioral)

Intent:

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

When to Use:

  • When you need to access an aggregate object’s contents without exposing its internal representation

  • When you want to support multiple traversals of aggregate objects

  • When you want to provide a uniform interface for traversing different aggregate structures

Prompt Example:

"Implementiere einen Iterator nach GoF für die benutzerdefinierte Baumstruktur, der verschiedene Traversierungsstrategien (Tiefe, Breite) unterstützt."