Chapter 7. Reaching into a Collection with the Iterator

In Chapter 6, we looked at composites—objects that appear to be simple components but are actually made up of a collection of subcomponents. Of course, an object does not have to be a composite to know about collections of other objects. An Employee object might know about several dependents, or phone numbers, or, in the case of a well-paid executive, the addresses of many palatial estates. In this kind of situation, it would be helpful if we could sequence through all of the sub-objects without needing to know any of the details of how the aggregate object is storing them.

In this chapter, we will explore the Iterator pattern, a technique that allows an aggregate object to provide the ...

Get Design Patterns in Ruby 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.