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. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access