avatarItchimonji

Summary

The Iterator pattern encapsulates an aggregate object and provides sequential access to its elements without exposing its internal structure, supporting polymorphic iteration and parallel traversal operations.

Abstract

The Iterator design pattern is a behavioral pattern that facilitates traversal of aggregate objects without revealing their underlying representations. It allows for the manipulation of elements within an aggregate, such as an array, through an intermediate layer that supports operations like adding, removing, or copying elements. This pattern is characterized by its ability to perform multiple simultaneous traversals and is commonly found in everyday applications like music playlists and TV channel surfing. The Iterator pattern simplifies the aggregate's interface and can adapt to different traversal algorithms, making it a versatile tool for developers. It is part of a broader set of design patterns that are essential for solving common programming problems, enhancing code readability, and maintaining consistency in system design.

Opinions

  • The Iterator pattern is considered very helpful for encapsulating aggregated objects and providing controlled access to their elements.
  • It is valued for its support of polymorphic iteration, which allows for the same iteration interface to be used across different data structures.
  • The pattern's ability to enable parallel execution of traversal operations is seen as a significant advantage, particularly in complex systems.
  • Real-life examples, such as MP3 player track switching and TV channel zapping, illustrate the practicality and widespread applicability of the Iterator pattern.
  • The pattern is part of essential base knowledge for developers, aiding in consistent communication about system design and serving as an introduction to object composition and dependency inversion principles.
  • Behavioral Patterns, including the Iterator pattern, are recommended for making complicated code more readable and maintainable, as well as facilitating quick implementation of extensions.

Iterator | Cheat Sheet

Behavioral Pattern — Design Patterns Series

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.

Code example on Github

Learn More

Resources

Design Pattern
Behavioral Pattern
Iterator
Object Composition
Cheatsheet
Recommended from ReadMedium