Iterator Design Pattern
Image taken from Medium: https://medium.com/unlearninglabs/cartoon-guide-to-data-structures-singly-linked-lists-9af7a6e64ad8

Iterator Design Pattern

Iterator design pattern is one of the most used design patterns. In this article we will discuss the what, why and how of the iterator pattern. We also discuss the implementation with the help of an email application example.

What?

A collection is just a group of elements arranged in an orderly way. Trees, Maps, Arrays, Lists are all collections. Iterators are tools that help us traverse the elements in a collection.

Why?

A single collection can be traversed in different ways. E.g. A list can be traversed from the front end or the rear end, a Tree can be traversed in BFS manner or DFS manner etc. We can try to code up all these different traversal logic in the collections itself but this approach has a few shortcomings:

  1. It's very difficult if not impossible to code for all kinds of traversal logic people may use in the containers.
  2. Coding this logic inside the Collection makes the class cluttered. This drives the focus away from the primary responsibility of a collection which is efficient storage of data.
  3. Traversal algorithms depend on the usage. Coding for all traversal algorithms in the generic class would lead to a lot of confusion.
  4. Some use cases may require simultaneous traversal of the collection in different ways. This just increases the complexity of coding all this logic in the collection class itself.
  5. Any addition of traversal algorithm means changing the Collection class. This makes the code open to changes and is not aligned with the open closed principal.

How?

Collections overcome the above mentioned shortcomings with the help of Iterators. There are several other benefits of using iterators that we'll discuss towards the end of this article. In addition to the traversal logic these iterators also contain all the information that is required for traversal like current position in the collection, size of collection, next element in collection etc.

Next let's look at how these iterators are implemented.

Consider that you are working on an email application. The users can view emails in two possible orders. 1) According to timestamp. 2) According to priority

First let's create an Iterator interface that all concrete iterators will implement. Also let's create the email iterator that implements this interface.

No alt text provided for this image

Next let's take a look at the EmailRepository Class. This is the class that creates and return the EmailIterator. Note that we have omitted other code from this class for simplicity.

No alt text provided for this image

Finally let's look at the client that calls the iterator and the repository.

No alt text provided for this image

Code Explanation

The client code contains the collection or the object that contains the collection. The client can either create this object itself or get this from outside. When traversal is required, the client asks this object for an iterator. This object returns a new iterator and passes the collection, as a reference, to the newly created iterator. Using this reference the iterator can access the elements of the collection. The client uses this iterator to iterate over the elements of the collection.

No alt text provided for this image

Advantages

Although it seems slightly complicated to use the iterator pattern but it offers several advantages that makes usage of this pattern worthwhile.

  1. This separates the traversal logic from the Collection, which makes the client code less coupled to the traversal code.
  2. This pattern can be used to hide, from the client, the complexity of the collection for simplicity or security and only give them simple methods to traverse it.
  3. Because all these iterators extend a common interface. The Collections in the code can be replaced easily without modifying the rest of the iteration logic. This also makes it possible to write code in terms of generics when the Collection type is not known.
  4. Iterators also make the code cleaner. If the same collection is used at different places, there is no need to duplicate the iteration logic everywhere.

I hope you liked this article. I regularly write such articles on #LLD and #GoodCodingPractices. Follow Prateek Mishra and learn something new every week.


References:

I took help from the following sources for writing this article:

  1. https://refactoring.guru/design-patterns/iterator
  2. https://www.jobsity.com/blog/a-deep-dive-into-9-popular-design-patterns-and-their-uses
  3. Image taken from https://medium.com/unlearninglabs/cartoon-guide-to-data-structures-singly-linked-lists-9af7a6e64ad8

要查看或添加评论,请登录

社区洞察

其他会员也浏览了