Skip to content

Commit 4983c26

Browse files
authored
Merge pull request #75 from ilopX/add-iterator-pattern
Add iterator pattern.
2 parents cf5a916 + 7a0c63d commit 4983c26

File tree

8 files changed

+116
-2
lines changed

8 files changed

+116
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 0.36.0
2+
- Add iterator pattern: Github Commit.
3+
14
## 0.35.0
25
- Add conceptual command pattern.
36

README.md

Lines changed: 1 addition & 1 deletion
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-
- [ ] **Iterator**
16+
- [x] **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)]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Template Method 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+
## Diagram:
8+
![image](https://user-images.githubusercontent.com/8049534/183165928-7274e761-09e3-48ce-b9c1-41d552fa1f1a.png)
9+
10+
11+
### Client code:
12+
```dart
13+
void main() async {
14+
final GitHubRepo gitHubRepo = await GitHubLoader.get(
15+
userName: 'RefactoringGuru',
16+
repoName: 'design-patterns-dart',
17+
);
18+
19+
print(
20+
'Iterate last 10 commits.'
21+
'\n----------------------------',
22+
);
23+
24+
for (Commit commit in gitHubRepo.commitIterator()) {
25+
print(commit.message);
26+
}
27+
}
28+
```
29+
30+
### Output:
31+
```
32+
Iterate last 10 commits.
33+
----------------------------
34+
Merge pull request #74 from ilopX/fix-conceptual-command-pattern-folder-name Fix conceptual command pattern folder name
35+
Fix conceptual command pattern folder name.
36+
Merge pull request #73 from ilopX/add-conceptual-command-pattern Add conceptual command pattern
37+
Bump version 0.35.0.
38+
Add README.
39+
Impl conceptual command pattern.
40+
Merge pull request #72 from ilopX/add-singleton-pattern Add singleton pattern.
41+
Bump version 0.34.0.
42+
Add README.
43+
```
44+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Commit {
2+
String message;
3+
4+
Commit(this.message);
5+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import 'dart:convert';
2+
import 'dart:io';
3+
import '../pattern/github_repo.dart';
4+
5+
class GitHubLoader {
6+
static Future<GitHubRepo> get({required userName, required repoName}) async {
7+
final url = Uri.http(
8+
'api.github.com',
9+
'repos/$userName/$repoName/commits',
10+
{'per_page': '10'},
11+
);
12+
final json = await _loadCommits(url);
13+
return GitHubRepo(json);
14+
}
15+
16+
static Future<List<dynamic>> _loadCommits(Uri url) async {
17+
final client = HttpClient();
18+
try {
19+
final response = await client.getUrl(url);
20+
final request = await response.close();
21+
final content = await request.transform(utf8.decoder).join();
22+
return jsonDecode(content);
23+
} finally {
24+
client.close();
25+
}
26+
}
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'github/commit.dart';
2+
import 'github/github_loader.dart';
3+
import 'pattern/github_repo.dart';
4+
5+
void main() async {
6+
final GitHubRepo gitHubRepo = await GitHubLoader.get(
7+
userName: 'RefactoringGuru',
8+
repoName: 'design-patterns-dart',
9+
);
10+
11+
print(
12+
'Iterate last 10 commits.'
13+
'\n----------------------------',
14+
);
15+
16+
for (Commit commit in gitHubRepo.commitIterator()) {
17+
print(commit.message);
18+
}
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import '../github/commit.dart';
2+
3+
class GitHubRepo {
4+
GitHubRepo(this._json);
5+
6+
Iterable<Commit> commitIterator() sync* {
7+
for (final jsonCommit in _json) {
8+
var message = jsonCommit['commit']['message'] as String;
9+
message = message.replaceAll(RegExp(r'\n+'), ' ');
10+
11+
yield Commit(message);
12+
}
13+
}
14+
15+
final List<dynamic> _json;
16+
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
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.35.0
3+
version: 0.36.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)