Design patterns: Iterator

Now I will discuss the Iterator pattern, as the name suggests, it is used to iterate through collections, objects, etc. This is, in a nutshell, more precisely described in the article.



  • Providing sequential access to an object or objects composed of a specified number of elements without revealing the internal representation of these objects.
  • Creation a unified interface for various aggregated (connected) structures is called polymorphic iteration support



Iterator pattern can be used eg in situations when you have iterated over various data structures of their elements, but it is illegible and the internal structure is revealed, then it is better to create one abstract interface for each structure which will define what it does and will be iterate over each structure in obvious way. And the client doesn’t need to know its exact implementation, it’s enough that this interface will only define what it does.


Use when:

  • You want to have an interface that will allow you moving on the collections, collections or objects in your application in a uniform and understandable way.
  • You want to access some collection or elements of some objects without revealing its exact implementation.



The Iterator pattern has recently lost its significance when generic types appeared, although it also depends on the technology we use in the project, but in most cases there are many platforms that have generic types, the generic ones have an iteration method that iterates over the elements of the collection, e.g. lists in .NET . Nevertheless, it is worth knowing this pattern to know how it works from the inside and that we can create new interfaces adapted to our requirements, which iterates over data elements.

Take a look at the example below:

Lists have already implemented the Iterator pattern, we use ready-made solutions and the foreach loop displays the elements of the collection one by one.

The result is this:



In the Structure section we have a UML diagram, consisting of:

A client that uses the ListCollection and MapCollection classes that are inherits after the abstract class Collection, which defines the createTraversalObject() method, the createTraversalObject() method creates the ListTraversal class, and the ListTraversal class inherits from the TraversalAbstraction interface, which has defines iterate methods.

Iterator example

The pattern of the Iterator in the code looks something like this:

This looks like similar as the UML diagram, we have defined the interface Collection, in which the createIterator method is defined (we called it differently than in the UML diagram createTraversalObject). We have a ListCollection class that implements the Collection interface and we use the method to create the ListTraversal class, the ListCollection class maybe remind you about implementation a other pattern if yes, you don’t mistake it. This is the factory method that is described halfway in the entry about the factory pattern.

The ListTraversal class implements methods which iterating after the collection are defined in the TraversalAbstraction interface. The hasNext() method checks whether the string on which we iterate, is not over, that we will not iterating through the empty collection, and the next() method checks with the hasNext() method that the collection is over, if not, we return another element from this collection.

The result is this:


List prototype from .NET

We will do more developed implementation of the Iterator pattern than the above, it will be such a prototype of list with .NET, because it will have some of its functionalities.

Let’s start with the class of saving values in the collection, in other words, aggregate.

And the MyIterator class with implemented iteration fields after the collection. After the names you can guess what they are doing.

Calling these classes in the client looks like this:

First, we save the data to the MyAggregate class, then call the GetIterator() method to create an instance of the MyIterator class so that we can operate on the MyAggregate class data, we operate on data in a for loop, in which we check the condition of terminating the loop with the field IsDone, element on which we operate in the loop whether is the last one in the collection if so then we finish the loop. And finally, we check how many elements are in the collection. Analyze this example calmly.



An example from life taken


Switching channels on the TV

For example, switching channels on the TV to the next or previous one.

Iterator example

In the code, we will do it as shown in the above figure. The code will look very much like the previous example.

The ChannelFrequencies interface as well as the TunedChannel class have the same logic as in the previous example of the IAggregate and MyAggregate classes, only the names of classes, methods and variables are different.

The ChannelIterator class also has almost the same logic as the previous example MyIterator class, I just only added the Previos field to it.

Only the customer is subject to quite a big change.

In the while loop, we check what the client has chosen, depending on the option selected, we move one space forward in the channel collection or back.



Relations with other design patterns

  • The iterator can iterate through the branches of the composite. Visitor can implement operations in the composite.
  • The so-called polymorphic iterators are based on the Factory methods to implement the iterator’s subclass.
  • The Iterator can be used together with the Memento pattern to capture the iteration state.



That’s all about on subject the Iterator pattern🙂

Link to github with all examples:

This content also you can find on my steemit blog

And on medium:

In the next article, we will talk about the Chain of Responsibility design pattern.

As a standard, I remind you about the newsletter, which I send notifications about new entries and additional information about the IT world in general.

Link to the site and group on fb:

– site on fb:ławomir Kowalski

– group on fb: DevmanCommunity

Ask, comment underneath at the end of the post, share it, rate it, whatever you want🙂. See you in a week, or rather in two weeks because I just don’t have a time make, I want have quality on this blog, not quantity, we’ll see how much time I will be have.🙂

If that post was useful for you share it with your friends :)

Post a comment

Notify about