CHAPTER 10

image

Iterator Patterns

GoF Definition: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

Concept

Iterators are generally used to traverse a container to access its elements.

Real-Life Example

Suppose there are two companies: Company A and Company B. Company A stores its employee records (name, etc.) in a linked list and Company B stores its employee data in a big array. One day the two companies decide to work together. The iterator pattern is handy in such a situation. We need not write codes from scratch. We’ll have a common interface through which we can access data ...

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