15. Iterator mode of design patterns

Iterator pattern of design patterns

table of Contents

Iterator pattern of design patterns

basic introduction

Notes and details of the iterator pattern


basic introduction

1) Iterator Pattern (Iterator Pattern) is a commonly used design pattern, which belongs to the behavioral pattern

2) If our collection elements are implemented in different ways, such as arrays, java collection classes, or other ways, when the client wants to traverse these collection elements, it will use multiple traversal methods, and It also exposes the internal structure of the element, which can be solved by using the iterator pattern.

3) The iterator mode provides a unified interface for traversing the collection elements, using a consistent method to traverse the collection elements, without knowing the underlying representation of the collection object, that is, not exposing its internal structure.

Notes and details of the iterator pattern

 Advantages

1) Provide a unified method to traverse objects, customers no longer need to consider the type of aggregation, and use one method to traverse objects.

2) The internal structure of the aggregation is hidden. When the client wants to traverse the aggregation, it can only get the iterator without knowing the specific composition of the aggregation.

3) Provides a design idea that a class should have only one cause of change (called the single responsibility principle). In the aggregation class, we separate the iterators, which is to separate the responsibility of managing the collection of objects and traversing the collection of objects, so that if the collection changes, only the aggregate objects will be affected. And if the traversal method changes, only the iterator is affected.

4) Use when you want to display a group of similar objects, or traverse a group of the same objects, iterator mode is suitable

 Disadvantages

Each aggregate object needs an iterator, which will generate multiple iterators. It is not good to manage the class

Guess you like

Origin blog.csdn.net/qq_45072383/article/details/114240517