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. ...
Get Design Patterns Java™ Workbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.