-
Notifications
You must be signed in to change notification settings - Fork 74
/
Iterator.js
77 lines (63 loc) · 1.69 KB
/
Iterator.js
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
72
73
74
75
76
77
'use strict';
class Iterator {
constructor() {
console.log('Iterator Class created');
}
first() {
console.log('Iterator.first invoked');
}
next() {
console.log('Iterator.next invoked');
}
isDone() {
console.log('Iterator.isDone invoked');
}
currentItem() {
console.log('Iterator.currentItem invoked');
}
}
class ConcreteIterator extends Iterator {
constructor(aggregate) {
super();
this.index = 0;
this.aggregate = aggregate;
console.log('ConcreteIterator Class created');
}
first() {
console.log('ConcreteIterator.first invoked');
return this.aggregate.list[0];
}
next() {
console.log('ConcreteIterator.next invoked');
this.index += 1;
return this.aggregate.list[this.index];
}
currentItem() {
console.log('ConcreteIterator.currentItem invoked');
return this.aggregate.list[this.index];
}
}
class Aggregate {
constructor() {
console.log('Aggregate Class created');
}
createIterator() {
console.log('Aggregate.CreateIterator invoked');
}
}
class ConcreteAggregate extends Aggregate {
constructor(list) {
super();
this.list = list;
console.log('ConcreteAggregate Class created');
}
createIterator() {
console.log('ConcreteAggregate.CreateIterator invoked');
this.iterator = new ConcreteIterator(this);
}
}
var aggregate = new ConcreteAggregate([0, 1, 2, 3, 4, 5, 6, 7]);
aggregate.createIterator();
console.log(aggregate.iterator.first());
console.log(aggregate.iterator.next());
console.log(aggregate.iterator.currentItem());