Yuxian: CSDN content partner, CSDN new star mentor, full-stack creative star creator, 51CTO (Top celebrity + expert blogger), github open source enthusiast (go-zero source code secondary development, game back-end architecture https: https://github.com/Peakchen)
The principle of the iterator pattern is explained in detail:
the iterator pattern (Iterator Pattern) is a behavioral design pattern, which is used to provide a unified way to traverse the elements in the collection object without exposing the internal structure of the collection object.
The core idea of the iterator pattern is to encapsulate the traversal operation on the collection into an independent iterator object, which can sequentially access the elements in the collection without knowing the specific implementation of the collection. In this way, the traversal algorithm of the collection object can be decoupled from the collection object itself, and different types of iterators can be provided to meet different traversal requirements.
The iterator pattern usually includes the following roles:
- Iterator (Iterator): defines the interface of the iterator, which contains methods for traversing the elements of the collection.
- Concrete Iterator: implement the iterator interface and implement a specific traversal algorithm.
- Collection (Collection): Defines the interface of collection objects, including methods for obtaining iterators.
- Concrete Collection: implement the collection interface and return a specific iterator object.
Underlying structure diagram:
The following is a classic structure diagram of the iterator pattern:
+------------------+ +------------------+ +------------------+
| Iterator | | Collection | | Client |
+------------------+ +------------------+ +------------------+
| +Next() |<-------->| +GetIterator() | | |
+------------------+ +------------------+ +------------------+
^
|
|
v
+------------------+
| ConcreteIterator |
+------------------+
| +collection: Collection | +------------------+
| +current: Element | | ConcreteCollection |
+------------------+ +------------------+
| +Next() | +GetIterator() |
+------------------+ +------------------+
In the above structure diagram, Iterator
is the iterator interface, which defines methods for traversing collection elements, such as Next()
.
Collection
Is the interface of the collection, which defines methods for obtaining iterators, such as GetIterator()
.
ConcreteIterator
It is a specific iterator class that implements Iterator
the interface and contains the corresponding collection objects.
ConcreteCollection
It is a specific collection class that implements Collection
the interface and returns a specific iterator object.
Scenario Explanation:
The iterator pattern is applicable to the following scenarios:
-
When you need to traverse a complex collection object and do not want to expose the internal structure of the collection object, you can use the iterator mode. The iterator mode encapsulates the traversal operation in the iterator object, and the client traverses through the iterator object without knowing the specific implementation of the collection object.
-
When you need to provide multiple traversal methods or customize the traversal algorithm, you can consider using the iterator mode. The iterator mode can define multiple different types of iterators, and each iterator can implement different traversal algorithms to meet different traversal requirements.
-
When special traversal methods such as reverse traversal and jump traversal need to be supported, the iterator mode can be used. The iterator pattern can implement these special traversal methods in the iterator without modifying the code of the collection object.
Code example implementation:
The following is an example of implementing the iterator pattern in Go language:
package main
import "fmt"
// Iterator 迭代器接口
type Iterator interface {
HasNext() bool
Next() interface{}
}
// Collection 集合接口
type Collection interface {
CreateIterator() Iterator
}
// ConcreteIterator 具体迭代器
typeConcreteIterator struct {
collection *ConcreteCollection
index int
}
func (iterator *ConcreteIterator) HasNext() bool {
return iterator.index < len(iterator.collection.elements)
}
func (iterator *ConcreteIterator) Next() interface{} {
if iterator.HasNext() {
element := iterator.collection.elements[iterator.index]
iterator.index++
return element
}
return nil
}
// ConcreteCollection 具体集合
type ConcreteCollection struct {
elements []interface{}
}
func (collection *ConcreteCollection) CreateIterator() Iterator {
return &ConcreteIterator{
collection: collection,
index: 0,
}
}
func main() {
// 创建具体集合对象
collection := &ConcreteCollection{
elements: []interface{}{"element1", "element2", "element3"},
}
// 创建迭代器
iterator := collection.CreateIterator()
// 遍历集合元素
for iterator.HasNext() {
element := iterator.Next()
fmt.Println(element)
}
}
In the above example, we defined Iterator
interfaces and Collection
interfaces. ConcreteIterator
It is a specific iterator class that implements Iterator
the interface and contains the corresponding collection objects and indexes. ConcreteCollection
It is a specific collection class that implements Collection
the interface and returns a specific iterator object.
In main
the function, we create a specific collection object collection
, then CreateIterator()
create an iterator object through the method iterator
, and finally use the iterator object to traverse the collection elements and print them out.
Documentation Links:
Here are some links to documentation on the Iterator pattern:
- Iterator pattern - Wikipedia
- Iterator Design Pattern - Refactoring Guru
- Design Patterns: Elements of Reusable Object-Oriented Software - This book is a classic in the field of design patterns and covers the iterator pattern in detail along with other design patterns.
What products are currently in use:
The iterator pattern is a classic design pattern that is widely used in various programming languages and software frameworks. Here are some common products and frameworks that use the iterator pattern or a similar iterator concept:
java.util.Iterator
Interfaces and related collection classes in Java , such asArrayList
, ,LinkedList
etc.System.Collections.IEnumerator
Interfaces and related collection classes in C# , such asList<T>
,LinkedList<T>
etc.- The iterator protocol and related iterable objects in Python, such as
list
,tuple
, ,dict
etc. - The iterator mode in the Go language is widely used in standard libraries and third-party libraries, such as
range
keywords for traversing data structures such as slices, maps, and channels. - Array methods such as
Array.prototype.forEach
, , and loop statements in JavaScript can be regarded as applications of the iterator pattern.Array.prototype.map
for...of