[General boards] [Winter 2019 courses] [Fall 2018 courses] [Summer 2018 courses] [Older or newer terms]

Iterator design pattern


#1

I am still am skeptical about the utility of the iterator design pattern.

From the example in class, why don’t we just have a method that returns the list called contacts and then iterate through contacts without having an address book iterator?

Below are the methods for AddressBookIterator. Since it appears that there is nothing unique about how methods hasNext and next are implemented, makes me believe that just having a normal iterator without implementing AddressBookIterator might make more sense? Please let me know.


#2

You are correct in that the example is contrived. It is meant to show how an iterator works, not convince you of the necessity of one, in this particular case. Often, you can use built in iterators for ArrayList, etc.