Learn the Iterator Design Pattern

Iterator is a behavioral design pattern that lets you traverse elements of a collection without exposing its underlying representation (list, stack, tree, etc.)

Iterator Design Pattern

UML Class Diagram

Not familiar with UML Class Diagram? I have written a detailed post on the UML Class diagram.

Implementation steps

  1. Create the iterator interface. It must have a method for fetching the next element from a collection.
  2. Define the collection interface and describe a method for getting iterators. The return type should correspond to that of the iterator interface.
  3. Create your collection classes by implementing the collection interface. The main idea is to provide the client with a shortcut for creating iterators, tailored to a particular collection class. The collection object must be passed to the iterator’s constructor to establish a link between them.
  4. Replace all collection traversal codes with iterators in the client code. Each time the client needs to iterate over collection elements, it fetches a new iterator object.

Source Code Implementation

When To Apply Iterator Design Pattern

  • You should use the Iterator pattern when your collection has a complex under-the-hood data structure, but you want to hide its complexity from clients.
  • Utilize the pattern to reduce duplication of traversal code across your application.
  • If you want your code to be able to traverse different data structures or if the types of these structures are unknown beforehand, then you should use the Iterator pattern.

Pros of Iterator Design Pattern

  • By separating bulky traversal algorithms into separate classes, you can simplify client code and collections.
  • Implementing new types of collections and iterators without breaking existing code is possible.
  • Each iterator object contains its own iteration state, so you can iterate over the same collection in parallel.

References

--

--

--

“Walking on water and developing software from a specification are easy if both are frozen”

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Map Expansion In Anito Legends Beta v0.3.1

Summary of GraphQL Finland 2018

Rambling to the God of Programming

Ansible and How Industries are Solving Challenges using it

🚨We have a very special announcement!🚨

Using a pre-seeded PostgreSQL docker image for faster CI/CD

How to add a CSS pre-loader animation to your website

665: The First Steps

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Neeraj Kushwaha

Neeraj Kushwaha

“Walking on water and developing software from a specification are easy if both are frozen”

More from Medium

What is CQS(Command Query Separation) Pattern?

How We Benefited From Webhooks in a Specific Use Case Scenario

Using Spring Data JPA

Aggregator Design pattern in Microservices