|
1 |
| -# 【迭代器设计模式详解】Java/JS/Go/Python/TS不同语言实现 |
| 1 | +# 【迭代器设计模式详解】C/Java/JS/Go/Python/TS不同语言实现 |
2 | 2 |
|
3 | 3 | # 简介
|
4 | 4 | 迭代器模式(Iterator Pattern),是一种结构型设计模式。给数据对象构建一套按顺序访问集合对象元素的方式,而不需要知道数据对象的底层表示。
|
|
18 | 18 | # UML
|
19 | 19 | <img src="../docs/uml/iterator-pattern.png">
|
20 | 20 |
|
21 |
| -# 代码 |
| 21 | +# Java代码 |
22 | 22 |
|
23 | 23 | ## 迭代器抽象接口
|
24 | 24 | ```java
|
@@ -63,7 +63,7 @@ public class ObjectIterator implements Iterator {
|
63 | 63 |
|
64 | 64 | ## 数据容器接口
|
65 | 65 | ```java
|
66 |
| -// 创建抽象容器接口,创建一个迭代器 |
| 66 | +// Container.go 创建抽象容器接口,创建一个迭代器 |
67 | 67 | public interface Container {
|
68 | 68 | public Iterator createIterator();
|
69 | 69 | }
|
@@ -116,9 +116,117 @@ public class ObjectList implements Container {
|
116 | 116 |
|
117 | 117 | // while循环迭代对象
|
118 | 118 | Iterator iter2 = objectList.createIterator();
|
| 119 | + objectList.setObjects(new Integer[] { 3, 5, 7, 9, 11 }); |
119 | 120 | while (iter2.hasNext()) {
|
120 | 121 | System.out.println(iter2.next());
|
121 | 122 | }
|
122 | 123 | ```
|
| 124 | + |
| 125 | +# Go代码 |
| 126 | + |
| 127 | +## 迭代器抽象接口 |
| 128 | +```go |
| 129 | +// Iterator.go 迭代器抽象接口,提供next和hasNext方法 |
| 130 | +type Iterator interface { |
| 131 | + HasNext() bool |
| 132 | + Next() string |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +## 具体迭代器 |
| 137 | +```go |
| 138 | +// ObjectIterator.go 对象迭代器,实现了抽象迭代器的方法,聚合了对象列表 |
| 139 | +type ObjectIterator struct { |
| 140 | + // 迭代器索引 |
| 141 | + index int |
| 142 | + // 聚合了数据对象 |
| 143 | + objectList *ObjectList |
| 144 | +} |
| 145 | + |
| 146 | +func (o *ObjectIterator) HasNext() bool { |
| 147 | + if o.index < o.objectList.Size() { |
| 148 | + return true |
| 149 | + } |
| 150 | + return false |
| 151 | +} |
| 152 | + |
| 153 | +func (o *ObjectIterator) Next() string { |
| 154 | + if o.HasNext() { |
| 155 | + // 返回数据对象提供的get方法,每访问一次下标增加1位 |
| 156 | + item := o.objectList.Get(o.index) |
| 157 | + o.index += 1 |
| 158 | + return item |
| 159 | + } |
| 160 | + return "" |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +## 数据容器接口 |
| 165 | +```go |
| 166 | +// Container.go 创建抽象容器接口,创建一个迭代器 |
| 167 | +type Container interface { |
| 168 | + CreateIterator() Iterator |
| 169 | +} |
| 170 | +``` |
| 171 | + |
| 172 | +## 具体数据对象 |
| 173 | +```go |
| 174 | +// ObjectList.go 对象列表,是一种数据容器,可以创建一个迭代器 |
| 175 | +type ObjectList struct { |
| 176 | + // 内部的数据结构 |
| 177 | + objects []string |
| 178 | +} |
| 179 | + |
| 180 | +func (o *ObjectList) CreateIterator() Iterator { |
| 181 | + fmt.Println("ObjectList::CreateIterator() [获取迭代器 ObjectIterator]") |
| 182 | + // 创建迭代器实例,绑定新建当前对象 |
| 183 | + return &ObjectIterator{ |
| 184 | + objectList: o, |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +func (o *ObjectList) SetObjects(objects []string) { |
| 189 | + o.objects = objects |
| 190 | +} |
| 191 | + |
| 192 | +func (o *ObjectList) GetObjects() []string { |
| 193 | + return o.objects |
| 194 | +} |
| 195 | + |
| 196 | +func (o *ObjectList) Size() int { |
| 197 | + return len(o.objects) |
| 198 | +} |
| 199 | + |
| 200 | +func (o *ObjectList) Get(index int) string { |
| 201 | + return o.objects[index] |
| 202 | +} |
| 203 | +``` |
| 204 | + |
| 205 | +## 测试调用 |
| 206 | +```go |
| 207 | + /* |
| 208 | + * 迭代器模式是给数据容器创建单独的迭代器,用来遍历里面的数据对象 |
| 209 | + * 数据容器和迭代器相互关联,外部通过迭代器来访问数据容器 |
| 210 | + * 通过这种方式由迭代器类来负责数据遍历,这样可以做到不暴露集合的内部结构 |
| 211 | + */ |
| 212 | + |
| 213 | + int i = 0; |
| 214 | + ObjectList objectList = new ObjectList(); |
| 215 | + objectList.setObjects(new String[] { "Thomas", "Merry", "Jack", "Tony", "Jerry", "Joey" }); |
| 216 | + // for循环迭代对象 |
| 217 | + for (Iterator iter = objectList.createIterator(); iter.hasNext();) { |
| 218 | + String name = (String) iter.next(); |
| 219 | + System.out.println("objectList[" + i + "] = " + name); |
| 220 | + i++; |
| 221 | + } |
| 222 | + |
| 223 | + // while循环迭代对象 |
| 224 | + Iterator iter2 = objectList.createIterator(); |
| 225 | + objectList.setObjects(new Integer[] { 3, 5, 7, 9, 11 }); |
| 226 | + while (iter2.hasNext()) { |
| 227 | + System.out.println(iter2.next()); |
| 228 | + } |
| 229 | +``` |
| 230 | + |
123 | 231 | ## 更多语言版本
|
124 | 232 | 不同语言实现设计模式:[https://github.com/microwind/design-pattern](https://github.com/microwind/design-pattern)
|
0 commit comments