Behavioral: Iterator Pattern

-- by Vikram

There are different types that allow you to maintain a list of items, for example an Array is one data structure that allows you to store the list of Objects, then there are structures like Map, Lists and so on. 

If you see each of them have similar purpose but then each of them provide some convenience over other. Iterator pattern aims to provide a consistent interface for iterating over these constructs. Which is very reasonable as and really makes sense to me. 

I would rather like to have this consistent across languages, but that can be little too much to desire for more or less the objective is that irrespective of which underlying data structure you are using your clients should be able to iterate over them by writing a code like below. 





There are no hard-rules defined on what these methods should be or what all functionality you should implement but there are these conventions of using hasNext() and next()  some languages or packages can provide an interface for the iterator pattern so that there is more and more consistency across implementation but the sole objective it that any sort of collections of objects or variables you should have a common iterable interface. Makes life so much easier and also when you need to work with multiple such collections a client can implement it's functionality without have to know what that underlying data structure is that holds these collections.