• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

HF Design Patterns Composite Iterator Question

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the same problem, except within the examples the book provides: Dessert menu items are printed twice (dessert menu is submenu of diner menu). I have a hard time tracing through the recursion to find the problem, but I've logged an errata/question with OReilly this afternoon.
 
author
Posts: 33
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys, this is a bug most definitely. Beth and I are in the middle of another book crunch right now, but we promise this will be our first priority when we come up for air (which won't be for a number of weeks). So sorry we can't jump right on this.

Aitor, sorry we're just seeing this!

Eric
 
Aitor Imaz
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eric, never mind.

Good to hear that you're working on a new book!
 
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic