The Iterator Pattern provides a way to sequentially access the elements of an aggregate object without exposing the object's internal implementation. This allows for accessing the internal data of the collection transparently from external code. The Iterator Pattern belongs to the behavioral design pattern.
The Iterator Pattern is designed for collection objects, which are likely to involve operations such as adding and deleting elements, as well as supporting the traversal of collection elements. At this point, we can place the traversal operation in the collection object, but in doing so, the collection object takes on too much responsibility. One of the principles of object-oriented design is the Single Responsibility Principle, so we should try to separate these responsibilities and use different classes to take on different responsibilities. The Iterator Pattern uses the iterator class to take on the responsibility of traversing the collection.
Support traversing a collection object in different ways, and can simplify the aggregate class.
Multiple traversals can be performed on the same aggregate.
In the Iterator Pattern, adding new aggregate classes and iterator classes is very convenient and does not require modifying the original code.
The Iterator Pattern allows accessing the content of an aggregate object without exposing its internal representation, that is, abstract iteration.
The Iterator Pattern provides a unified interface for traversing different collection structures, thus supporting the same algorithm to operate on different collection structures.
The Iterator Pattern separates the responsibilities of storing and traversing data, requiring the corresponding increase in the number of iterator classes when adding new aggregate classes, which increases the complexity of the system to some extent.
Modifying the structure of the collection while iterating with the iterator may lead to exceptions, and it is not possible to change the number of elements in the collection while iterating.