-
Notifications
You must be signed in to change notification settings - Fork 184
/
iterator.go
71 lines (59 loc) · 1.12 KB
/
iterator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package iterator
import "fmt"
/*
设计思想:
1. Iterator结构体
实现Next() HasNext()方法
2. Container容器
容器实现添加 移除Visitor 和
*/
//创建迭代器
type Iterator struct {
index int
Container
}
func (i *Iterator) Next() Visitor {
fmt.Println(i.index)
visitor := i.list[i.index]
i.index += 1
return visitor
}
func (i *Iterator) HasNext() bool {
if i.index >= len(i.list) {
return false
}
return true
}
//创建容器
type Container struct {
list []Visitor
}
func (c *Container) Add(visitor Visitor) {
c.list = append(c.list, visitor)
}
func (c *Container) Remove(index int) {
if index < 0 || index > len(c.list) {
return
}
c.list = append(c.list[:index], c.list[index+1:]...)
}
//创建Visitor接口
type Visitor interface {
Visit()
}
//创建具体的visitor对象
type Teacher struct {}
type Analysis struct {}
func (t *Teacher) Visit() {
fmt.Println("this is teacher visitor")
}
func (a *Analysis) Visit() {
fmt.Println("this is analysis visitor")
}
//工厂方法创建迭代器
func NewIterator() *Iterator {
return &Iterator{
index: 0,
Container: Container{},
}
}