Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a circular dependency between an collection and its iterator?

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:

enter image description here

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.

like image 592
Geek Avatar asked Oct 22 '22 15:10

Geek


1 Answers

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"

like image 96
Pete B. Avatar answered Oct 27 '22 09:10

Pete B.