Skip to content

Add conceptual iterator pattern. #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.37.0
- Add iterator pattern: Word Iterator.

## 0.36.0
- Add iterator pattern: Github Commit.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ It contains **Dart** examples for all classic **GoF** design patterns.
- [x] **Chain of Responsibility** - [[Server Middleware](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/chain_of_responsibility/server_middleware)]
- [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)]
- [x] **Interpreter** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/interpreter/conceptual)]
- [x] **Iterator** - [[Github Commit](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/iterator/github_commit)]
- [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)]
- [x] **Mediator** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/mediator/conceptual)]
- [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)]
- [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)]
Expand Down
47 changes: 47 additions & 0 deletions patterns/iterator/word_iterator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Iterator pattern
Iterator is a behavioral design pattern that lets you traverse elements of a collection without
exposing its underlying representation (list, stack, tree, etc.).

Tutorial: [here](https://refactoring.guru/design-patterns/iterator).

### Client code:
```dart
void main() {
final text = Text(
'Iterator is a behavioral design pattern that lets you traverse elements '
'of a collection without exposing its underlying representation '
'(list, stack, tree, etc.).',
);

for (final s in text) {
print(s);
}
}
```

**Output:**
```
Iterator
is
a
behavioral
design
pattern
that
lets
you
traverse
elements
of
a
collection
without
exposing
its
underlying
representation
list
stack
tree
etc
```
13 changes: 13 additions & 0 deletions patterns/iterator/word_iterator/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'text/text.dart';

void main() {
final text = Text(
'Iterator is a behavioral design pattern that lets you traverse elements '
'of a collection without exposing its underlying representation '
'(list, stack, tree, etc.).',
);

for (final s in text) {
print(s);
}
}
48 changes: 48 additions & 0 deletions patterns/iterator/word_iterator/pattern/word_iterator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import '../text/text.dart';

class WordIterator extends Iterator<String> {
WordIterator(this._text);

@override
String get current => _currWord!;

@override
bool moveNext() {
final start = _lastIndex;

while (_searchNextSpaceChar(' ')) {
// ++
}

_currWord = _getWord(start, _lastIndex);
return _currWord!.isNotEmpty;
}

final Text _text;
int _lastIndex = 0;
String? _currWord;

bool _searchNextSpaceChar(String char) {
final isTextEnd = _lastIndex >= _text.text.length;

if (isTextEnd) {
return false;
}

final isNotSpaceChar = _text.text[_lastIndex++] != char;
return isNotSpaceChar;
}

String _getWord(int start, int end) {
final noWordChars = RegExp(r'\W');
return _text.text
.substring(
start,
_lastIndex,
)
.replaceAll(
noWordChars,
'',
);
}
}
10 changes: 10 additions & 0 deletions patterns/iterator/word_iterator/text/text.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import '../pattern/word_iterator.dart';

class Text extends Iterable<String> {
final String text;

Text(this.text);

@override
Iterator<String> get iterator => WordIterator(this);
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: design_patterns_dart
description: Dart examples for all classic GoF design patterns.
version: 0.36.0
version: 0.37.0
homepage: https://refactoring.guru/design-patterns
repository: https://github.com/RefactoringGuru/design-patterns-dart
issue_tracker: https://github.com/RefactoringGuru/design-patterns-dart/issue
Expand Down