Skip to content

Commit d35a5c9

Browse files
authored
Merge pull request #78 from ilopX/add-conceptual-iterator-pttern
Add conceptual iterator pattern.
2 parents 435e111 + 46504b5 commit d35a5c9

File tree

7 files changed

+123
-2
lines changed

7 files changed

+123
-2
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 0.37.0
2+
- Add iterator pattern: Word Iterator.
3+
14
## 0.36.0
25
- Add iterator pattern: Github Commit.
36

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ It contains **Dart** examples for all classic **GoF** design patterns.
1313
- [x] **Chain of Responsibility** - [[Server Middleware](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/chain_of_responsibility/server_middleware)]
1414
- [x] **Command** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/command/conceptual)] [[Text Editor](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/command/text_editor)]
1515
- [x] **Interpreter** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/interpreter/conceptual)]
16-
- [x] **Iterator** - [[Github Commit](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/iterator/github_commit)]
16+
- [x] **Iterator** - [[Word Iterator](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/iterator/word_iterator)] [[Github Commit](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/iterator/github_commit)]
1717
- [x] **Mediator** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/mediator/conceptual)]
1818
- [x] **Memento** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/memento/conceptual)] [[![16x16](https://user-images.githubusercontent.com/8049534/171852337-57db0faf-1f5e-489a-a79a-22ed4f47b4ed.png) Memento Editor](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/memento/memento_editor)]
1919
- [x] **Observer** - [[Open-Close Editor Events](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/observer/open_close_editor_events)] [[AppObserver](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/observer/app_observer)] [[![16x16](https://user-images.githubusercontent.com/8049534/171852337-57db0faf-1f5e-489a-a79a-22ed4f47b4ed.png) Subscriber Flutter Widget](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/observer/subscriber_flutter_widget)]
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Iterator pattern
2+
Iterator is a behavioral design pattern that lets you traverse elements of a collection without
3+
exposing its underlying representation (list, stack, tree, etc.).
4+
5+
Tutorial: [here](https://refactoring.guru/design-patterns/iterator).
6+
7+
### Client code:
8+
```dart
9+
void main() {
10+
final text = Text(
11+
'Iterator is a behavioral design pattern that lets you traverse elements '
12+
'of a collection without exposing its underlying representation '
13+
'(list, stack, tree, etc.).',
14+
);
15+
16+
for (final s in text) {
17+
print(s);
18+
}
19+
}
20+
```
21+
22+
**Output:**
23+
```
24+
Iterator
25+
is
26+
a
27+
behavioral
28+
design
29+
pattern
30+
that
31+
lets
32+
you
33+
traverse
34+
elements
35+
of
36+
a
37+
collection
38+
without
39+
exposing
40+
its
41+
underlying
42+
representation
43+
list
44+
stack
45+
tree
46+
etc
47+
```
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import 'text/text.dart';
2+
3+
void main() {
4+
final text = Text(
5+
'Iterator is a behavioral design pattern that lets you traverse elements '
6+
'of a collection without exposing its underlying representation '
7+
'(list, stack, tree, etc.).',
8+
);
9+
10+
for (final s in text) {
11+
print(s);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import '../text/text.dart';
2+
3+
class WordIterator extends Iterator<String> {
4+
WordIterator(this._text);
5+
6+
@override
7+
String get current => _currWord!;
8+
9+
@override
10+
bool moveNext() {
11+
final start = _lastIndex;
12+
13+
while (_searchNextSpaceChar(' ')) {
14+
// ++
15+
}
16+
17+
_currWord = _getWord(start, _lastIndex);
18+
return _currWord!.isNotEmpty;
19+
}
20+
21+
final Text _text;
22+
int _lastIndex = 0;
23+
String? _currWord;
24+
25+
bool _searchNextSpaceChar(String char) {
26+
final isTextEnd = _lastIndex >= _text.text.length;
27+
28+
if (isTextEnd) {
29+
return false;
30+
}
31+
32+
final isNotSpaceChar = _text.text[_lastIndex++] != char;
33+
return isNotSpaceChar;
34+
}
35+
36+
String _getWord(int start, int end) {
37+
final noWordChars = RegExp(r'\W');
38+
return _text.text
39+
.substring(
40+
start,
41+
_lastIndex,
42+
)
43+
.replaceAll(
44+
noWordChars,
45+
'',
46+
);
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import '../pattern/word_iterator.dart';
2+
3+
class Text extends Iterable<String> {
4+
final String text;
5+
6+
Text(this.text);
7+
8+
@override
9+
Iterator<String> get iterator => WordIterator(this);
10+
}

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: design_patterns_dart
22
description: Dart examples for all classic GoF design patterns.
3-
version: 0.36.0
3+
version: 0.37.0
44
homepage: https://refactoring.guru/design-patterns
55
repository: https://github.com/RefactoringGuru/design-patterns-dart
66
issue_tracker: https://github.com/RefactoringGuru/design-patterns-dart/issue

0 commit comments

Comments
 (0)