
Go 迭代器模式讲解和代码示例
迭代器是一种行为设计模式, 让你能在不暴露复杂数据结构内部细节的情况下遍历其中所有的元素。
在迭代器的帮助下, 客户端可以用一个迭代器接口以相似的方式遍历不同集合中的元素。
概念示例
迭代器模式的主要思想是将集合背后的迭代逻辑提取至不同的、 名为迭代器的对象中。 此迭代器提供了一种泛型方法, 可用于在集合上进行迭代, 而又不受其类型影响。
collection.go: 集合
package main
type collection interface {
createIterator() iterator
}
userCollection.go: 具体集合
package main
type userCollection struct {
users []*user
}
func (u *userCollection) createIterator() iterator {
return &userIterator{
users: u.users,
}
}
iterator.go: 迭代器
package main
type iterator interface {
hasNext() bool
getNext() *user
}
userIterator.go: 具体迭代器
package main
type userIterator struct {
index int
users []*user
}
func (u *userIterator) hasNext() bool {
if u.index < len(u.users) {
return true
}
return false
}
func (u *userIterator) getNext() *user {
if u.hasNext() {
user := u.users[u.index]
u.index++
return user
}
return nil
}
user.go: 客户端代码
package main
type user struct {
name string
age int
}
main.go: 客户端代码
package main
import "fmt"
func main() {
user1 := &user{
name: "a",
age: 30,
}
user2 := &user{
name: "b",
age: 20,
}
userCollection := &userCollection{
users: []*user{user1, user2},
}
iterator := userCollection.createIterator()
for iterator.hasNext() {
user := iterator.getNext()
fmt.Printf("User is %+v\n", user)
}
}
output.txt: 执行结果
User is &{name:a age:30}
User is &{name:b age:20}