I could find out that its not a bug in the Java API, its a bug in the logic of the program rather. Its related to pushing the composite iterator into the stack. The bug is that for every additional level of nesting that you introduce, that level will be traversed that many number of times. ie If you have a third level of nesting, that level will be traversed 3 times (instead of once). There is a redundancy in pushing the composite iterator into the stack. I tried to work out the fix for that code, but things started going above my head and I quit
Joined: Aug 19, 2005
Considering the dual node/leaf nature of a Composite component the assumption that the iterator doesn't iterate over the node that produced it (the root of the (sub)tree) seems strange. Once you accept that the iterator has to iterate over the root node of the tree, it also becomes clear that that leaf node cannot return a null iterator - instead it must return an iterator that iterates only over the leaf node itself (MenuItem):
In the case of a non-leaf node the node itself (i.e. the root of that subtree) has to be supplied (Menu):
The following is only a first pass for the iterator - so it might still contain bugs (i.e. it hasn't been unit tested) or there may be a (much) better way to do it:
[ March 29, 2006: Message edited by: Peer Reynders ]
I'm the person who submitted the bug report to O'Reilly about the problem with Composite Iterator. I thought that my comments would go to the authors, which is why I made them sketchy. If I knew that they were being sent out to the public, then I would have been more clear.
The basic idea is that there are two kinds of iteration going on in the code: the iterator method that clients call, and the iterator that gets the children of a composite node. You cannot use the same method to do both things. (And even if you could, it would be bad style.) So I introduce the method childIterator to do the latter iteration. Once you see the need for two distinct methods, the code is pretty straightforward to write, and hopefully straightforward to read.
Anyway, I taught an OOP course here at Boston College using the HeadFirst design Patterns book. It's a great book. But the Composite Iterator chapter is very badly written and wrong in several places. The URL for my course is http://www.cs.bc.edu/~sciore/courses/cs353/coverage.html My explanation of the problem and solution is towards the end of the lecture notes for class #23. My code appears in the "Menu Iterator" handout from that class.
Comments on that class or any others are appreciated.