Design Pattern, Software Development

The Iterator Design Pattern in Simple Words

The Iterator design pattern is a behavioral design pattern that simplifies access to an object’s data collections. Moreover, it aims to provide a simple mechanism to make the data available. As a result, the client codes do not need to bother with the complexities of the internal data.

An Object With Complex Collections of Data

Suppose we have an object representing an extended family tree, and we want to list all the members’ names. Also, the data is in a hierarchical structure, meaning the object’s internal data structure is a tree or graph. Therefore, we cannot simply go through the data as we would with a list of items.

The Iterator Design Pattern

With the Iterator design pattern, we can provide a mechanism that makes the data available more straightforward, e.g., a list. As a result, the client codes do not need to traverse a tree. Instead, they receive a simplified version of the information. Alternatively, they could also receive a computed value like the sum, average, or status of the internal data.

The Iterator Design Pattern Off The Gang of Four

The Gang of Four book states the Iterator design pattern’s intent succinctly.

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

The aggregate object represents an object with data in a very complex structure or format (underlying representation). For example, the data could be in a tree or a composite of multiple various structures. Using the Iterator pattern, we intend to provide more straightforward access to an aggregate object’s data, usually in sequential form. However, we can also represent those elements differently, like their sum or some status.

Other Design Patterns

The Iterator design pattern is only one of the behavioral design patterns we can use to create maintainable software applications. Check out the other design patterns on Design Patterns Are The Ultimate Toolkit For Programmers.

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like