Everyone knows the Iterator interface in Java but outside the Collections of java the pattern is not often used.
The Iterator pattern is one of the patterns described in the GoF-book. According to them this pattern:
Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
Iterator pattern enhances the encapsualtion and promotes single responsibility.
Now let’s look at an example of how this pattern can be used. For example a data source provides some result set when you call it. This data source could be a database or a web service. Most often the data source interface will have a method like this:
When the result set is rather large or when we want to retrieve all the data an additional Paging parameter is added. The paging object will contain information about the limit and offset to use.
The code calling and consuming the result sets would look like this:
The problem with this code is that it does more than one thing. Not only does it consume the result set, it also needs to know how to call the data provider for a next batch of data. Now how can this be improved?
We can use the Iterator pattern to split the business logic and the looping logic.
First we create a new data source interface that returns an Iterator instead of a List.
The Iterator can be backed by a paged implementation similar to the one before.
Lastly the user of the interface can now focus on business logic instead of both the paging and the business logic.