Hi,
Let me first thank Eric & Elisabeth for writing such an engaging and entertaining book. I have tried reading the GoF book a few times but never made it from start to finish because I always found it really boring. I've only read the parts I've needed. On the other hand, I read the whole HFDP book in a snap, from the first page to the last, and came to understand many concepts from the GoF book much better. Now I have both of them as a reference
I needed a menu system for a project I'm working on with a tree containing folders and items so I implemented the Composite Iterator
pattern as described on pages 354-377 in the book. It was really simple, but I found myself with a problem.
If I execute an operation like the print() used on the example on a composite object, everything works fine. If all I want is to obtain an Iterator of "Menu Components" (in my case, all the folders and items corresponding to the tree) executing createIterator() on the root folder then I get duplicate elements. These duplicate elements show up if the tree has a depth bigger than one level (excluding the root folder). From what I can see, this is because the elements get pushed on the stack more than once.
My question is: is this behaviour by design or a bug in the implementation? If it's by design, is there a way to modify the code to avoid having duplicate elements in the Iterator? I can always look for duplicates in the calling code, but that's undesired in my opinion.
Thanks in advance!
Aitor