Behavioral

Iterator

Description

Lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.).

Where to use

  • Traversing complex data structures.

Real World Example

The 'foreach' loop in most languages. It works on Arrays, Lists, and Trees identically.

Code Example

class Iterator:\n    def __init__(self):\n        pass\n\n    def execute(self):\n        print('Executing Iterator')