O'Reilly logo

Design Patterns Java™ Workbook by Steven John Metsker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Iterating Over a Composite

In Challenge 5.3, from Chapter 5, Composite, you developed recursive behaviors that execute by traversing a composite structure. Iterators introduce a new challenge when working with composites, because you want to return control to a client as you traverse each node.

You might think that you would need a new iterator class for each domain-specific composite you model. In fact, you can design a fairly reusable composite iterator, although you have to arrange for domain classes to be able to instantiate your iterator. You may also need to handle composites that contain cycles.

As an example of a composite structure that you might want to iterate over, recall the ProcessComponent hierarchy that Chapter 5 introduced. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required