Skip to content

Commit 10cfd26

Browse files
committed
feat: iterator design
1 parent 71317c6 commit 10cfd26

File tree

8 files changed

+438
-343
lines changed

8 files changed

+438
-343
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ We welcome all contributions. You can submit any ideas as [pull requests](https:
130130

131131
[Data Structure Visualizations](https://www.cs.usfca.edu/~galles/visualization/Algorithms.html)
132132

133+
[javascript-algorithms](https://github.com/trekhleb/javascript-algorithms)
134+
133135
- sort
134136

135137
[JS-Sorting-Algorithm](https://github.com/Rain120/JS-Sorting-Algorithm)

plans.svg

Lines changed: 345 additions & 343 deletions
Loading

plans.xmind

14.4 KB
Binary file not shown.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
### 迭代器模式
2+
3+
迭代器模式(`Iterator Pattern`)是最简单的设计模式之一。它可以让用户透过特定的接口访问容器中的每一个元素而不用了解底层的实现。
4+
5+
迭代器模式属于行为型模式。
6+
7+
8+
### UML 类图
9+
10+
##### 传统的 Java 类图
11+
12+
![iterator-pattern.png](./images/iterator-pattern.png)
13+
14+
##### JavaScript 类图
15+
16+
![js-iterator-pattern.png](./images/js-iterator-pattern.png)
17+
18+
### 作用
19+
20+
不同的方式来遍历整个整合对象。
21+
22+
### 优缺点
23+
24+
##### 优点
25+
26+
- 支持以不同的方式遍历一个聚合对象。
27+
28+
- 迭代器简化了聚合类。
29+
30+
- 在同一个聚合上可以有多个遍历。
31+
32+
- 在迭代器模式中,增加新的聚合类和迭代器类都很方便,无须修改原有代码。
33+
34+
##### 缺点
35+
36+
- 由于迭代器模式将存储数据和遍历数据的职责分离,增加新的聚合类需要对应增加新的迭代器类,类的个数成对增加,这在一定程度上增加了系统的复杂性
37+
38+
### 场景
39+
40+
- [for-of](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for...of)
41+
42+
- [Iterator](http://es6.ruanyifeng.com/#docs/iterator)
43+
44+
- [Map, Set](http://es6.ruanyifeng.com/#docs/set-map)
45+
46+
### 参考
47+
48+
[迭代器模式](https://zh.wikipedia.org/zh-cn/%E8%BF%AD%E4%BB%A3%E5%99%A8%E6%A8%A1%E5%BC%8F)
234 KB
Loading
341 KB
Loading
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @Author: Rainy
3+
* @Date: 2019-11-14 19:25:01
4+
* @LastEditors: Rainy
5+
* @LastEditTime: 2019-12-12 20:29:44
6+
*/
7+
8+
import { NumberArrayMap } from 'types'
9+
10+
class Container {
11+
list: NumberArrayMap;
12+
13+
constructor(list: NumberArrayMap) {
14+
this.list = list
15+
}
16+
17+
getIterator(): Iterator {
18+
return new Iterator(this);
19+
}
20+
}
21+
22+
class Iterator {
23+
private _list: NumberArrayMap;
24+
private _index: number;
25+
26+
constructor(container: Container) {
27+
this._index = 0;
28+
this._list = container.list;
29+
}
30+
31+
next(): number | null {
32+
if (this.hasNext()) {
33+
return this._list[this._index++];
34+
}
35+
return null;
36+
}
37+
38+
hasNext(): boolean {
39+
return this._index >= this._list.length;
40+
}
41+
}

zh-CN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@
131131

132132
[Data Structure Visualizations](https://www.cs.usfca.edu/~galles/visualization/Algorithms.html)
133133

134+
[javascript-algorithms](https://github.com/trekhleb/javascript-algorithms)
135+
134136
- 排序
135137

136138
[十大经典排序算法](https://github.com/Rain120/JS-Sorting-Algorithm)

0 commit comments

Comments
 (0)