-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_iteration.dart
67 lines (53 loc) · 1.67 KB
/
data_iteration.dart
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
/// Iterator pattern is very commonly used design pattern in Java and .Net programming environment.
/// This pattern is used to get a way to access the elements of a collection object in sequential manner without any need to know its underlying representation.
library;
/// Implementation
/// We're going to create a Iterator interface which narrates navigation method and a Container interface which returns the iterator.
/// Concrete classes implementing the Container interface will be responsible to implement Iterator interface and use it
/// Step 1: Create interfaces.
abstract interface class Iterator<T> {
bool hasNext();
T? next();
}
abstract interface class Container<T> {
Iterator getIterator();
}
/// Step 2: Create concrete class implementing the Container interface.
/// This class has inner class NameIterator implementing the Iterator interface.
class NameRepository implements Container {
@override
Iterator getIterator() {
return NameIterator();
}
}
class NameIterator implements Iterator {
List<String> names = ["Robert", "John", "Julie", "Lora"];
int index = 0;
@override
bool hasNext() {
if (index < names.length) {
return true;
}
return false;
}
@override
Object? next() {
if (hasNext()) {
return names[index++];
}
return null;
}
}
/// Step 3: Use the NameRepository to get iterator and print names.
void main() {
NameRepository namesRepository = NameRepository();
for (Iterator iter = namesRepository.getIterator(); iter.hasNext();) {
String name = iter.next();
print("Name : $name");
}
}
/// Step 4: Verify the output.
/// Name : Robert
/// Name : John
/// Name : Julie
/// Name : Lora