Iterator | Cheat Sheet

Behavioral Pattern — Design Patterns Series

Itchimonji
CP Massive Programming
4 min readAug 2, 2021

--

The Iterator pattern is very helpful when encapsulating an aggregated object and providing sequential access to its elements. Shortly explained, you can not simply manipulate an array by copying, adding, or removing elements without an intermediate layer. In most cases a polymorphic iteration is supported. Additionally, a parallel execution of several traversal operations is possible (iterator monitors its traversal state itself and thus several operations can be executed simultaneously)

You can find this pattern in real life when switching tracks on your MP3 player or when zapping through TV channels. Many frameworks already have implemented the iterator themselves and it is available to the developer, for example TypeScript.

Real-life examples

  • Iterating through a music playlist
  • Zapping through TV channels
  • Searching for a radio channel

Meaning

  • Providing sequential access to elements of an aggregated object without exposing its underlying structure
  • Example: arrays can no longer be accessed via getter

Applicability (suitable if …)

  • Access to contents of an aggregate object without revealing its internal representation is supposed to be ensured
  • Multiple simultaneous traversals are supposed to be supported by aggregated objects
  • A uniform interface for traversing different composite structures is supposed to be provided (e.g. polymorphic iteration)

Assets and Drawbacks

  • Support of different traversing variants (change of traversing algorithms) e.g. by generics
  • Simplification of the aggregate interface
  • Parallel execution of several traversal operations (iterator monitors its traversal state itself and thus several ones can be executed simultaneously)

Example

As a simple example you can encapsulate a string array. First of all you need an Iterator interface that implements a generic type.

As a counterpart you implement an Aggregator to add or remove items, and expose the Iterator with the concrete type.

Behind the Aggregator interface you can hide a string collection which you can add items to and also remove items from. Via a factory the Iterator interface will be published.

With a concrete Iterator you can traverse through the array and publish some array information like current position or value.

Now, the client can simply add values, and as long as the current position is valid, the current value can be read.

Conclusion

Design Patterns are an important resource and base knowledge for every developer — they are very helpful for solving programmatic problems, help with consistent communication with other developers about system design, and serve as a significant introduction into object composition (besides inheritance) and dependency inversion.

Behavioral Patterns deal with algorithms and assignment of responsibilities to objects. Furthermore, they describe mutual communication patterns and grasp complex program sequences. They are therefore very well suited for use in areas where complicated code needs to be made readable and maintainable. Extensions are, in consequence, very good and quickly implementable.

--

--

Itchimonji
CP Massive Programming

Freelancer | Site Reliability Engineer (DevOps) / Kubernetes (CKAD) | Full Stack Software Engineer | https://patrick-eichler.com/links