An iterator definitely has to know about the collection that it has to iterate. Do the collection also have to know about its iterator? Is the dependency one way or both ways? If the collection has knowledge about a particular iterator implementation then it may not be possible to have different iterator strategies for the same collection. This is my thought. Am I right or did I get it completely wrong?
The GoF book has this diagram for the Iterator pattern:
I think it implies a circular dependency. I am not sure about the dotted lines from List
to LisIterator
and SkipList
to SkipListIterator
.
P.S: I deal in Java everyday but may be it is more of a design question than being specific to a particular language.
For all but some specialized implementations the collection does not need to know about the iterator.
The dotted line in this case signifies instantiation. If you have the gang of 4 book, the explantion is given on page 15, section 1.6.
Specifically, an iterator is created by the CreateIterator method, in Java iterator().
Geek Humor: tell people your Jersey Shore name is "THE INSTANTATION"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With