Skip to content

Commit 28e5cc0

Browse files
authored
Merge pull request #68 from ilopX/add-view-strategy
Add strategy pattern: View Strategy Example.
2 parents 45429cd + 1295176 commit 28e5cc0

File tree

10 files changed

+169
-2
lines changed

10 files changed

+169
-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.30.0
2+
- Add strategy pattern: View Strategy.
3+
14
## 0.29.0
25
- Add conceptual visitor pattern.
36

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ It contains **Dart** examples for all classic **GoF** design patterns.
2020
- [x] **State** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/state/three_state)] [[![16x16](https://user-images.githubusercontent.com/8049534/171852337-57db0faf-1f5e-489a-a79a-22ed4f47b4ed.png) State Manipulator](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/state/manipulator_state)]
2121
- [x] **Template Method** - [[Data Miner](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/template_method/data_miner)]
2222
- [X] **Visitor** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/visitor/conceptual)] [[Shape XML Exporter](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/visitor/shapes_exporter)]
23-
- [X] **Strategy** [[Reservation Cargo Spaces](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/strategy/reservation_cargo_spaces)]
23+
- [X] **Strategy** [[View Strategy](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/strategy/view_strategy)] [[Reservation Cargo Spaces](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/strategy/reservation_cargo_spaces)]
2424
- [ ] **Structural**
2525
- [x] **Adapter** - [[Text Graphics](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/adapter/text_graphics)] [[Square Round conflict](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/adapter/square_round_conflict)] [[![16x16](https://user-images.githubusercontent.com/8049534/171852337-57db0faf-1f5e-489a-a79a-22ed4f47b4ed.png) Flutter Adapter](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/adapter/flutter_adapter)]
2626
- [x] **Bridge** - [[Remote Device Control](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/bridge/devices_remote_control)] [[Clock](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/bridge/clock)]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Strategy Pattern
2+
Strategy is a behavioral design pattern that lets you define a family of algorithms, put each of
3+
them into a separate class, and make their objects interchangeable.
4+
5+
Tutorial: [here](https://refactoring.guru/design-patterns/strategy).
6+
7+
## Diagram:
8+
![image](https://user-images.githubusercontent.com/8049534/175562829-c91fbb12-50ac-4373-a33f-900527383a6c.png)
9+
10+
## Client code:
11+
```dart
12+
void main() {
13+
final byteList = ByteContext()
14+
..add('Hello guru')
15+
..add(123456789)
16+
..add(3.1456564984);
17+
18+
final strFormat = byteList.toStringView(StrViewStrategy());
19+
final hexFormat = byteList.toStringView(HexViewStrategy());
20+
final zipFormat = byteList.toStringView(ZipViewStrategy());
21+
22+
print(strFormat);
23+
print(hexFormat);
24+
print(zipFormat);
25+
}
26+
```
27+
28+
### Output:
29+
```
30+
StrViewStrategy:
31+
Hello guru, 123456789, 3.1456564984
32+
33+
HexViewStrategy:
34+
Hello guru : 48 65 6c 6c 6f 20 67 75 72 75
35+
123456789 : 00 00 00 00 07 5b cd 15
36+
3.1456564984 : 40 09 2a 4d f4 48 9f 7e
37+
38+
ZipViewStrategy:
39+
1f 8b 08 00 00 00 00 00 00 0a f3 48 cd c9 c9 57
40+
48 2f 2d 2a d5 51 30 34 32 36 31 35 33 b7 b0 d4
41+
51 30 d6 33 04 32 4d cd 4c 2c 2d 4c b8 00 d4 70
42+
cf ee 24 00 00 00
43+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import 'pattern/byte_context.dart';
2+
import 'strategies/hex_view_strategy.dart';
3+
import 'strategies/str_view_strategy.dart';
4+
import 'strategies/zip_view_strategy.dart';
5+
6+
void main() {
7+
final byteList = ByteContext()
8+
..add('Hello guru')
9+
..add(123456789)
10+
..add(3.1456564984);
11+
12+
final strFormat = byteList.toStringView(StrViewStrategy());
13+
final hexFormat = byteList.toStringView(HexViewStrategy());
14+
final zipFormat = byteList.toStringView(ZipViewStrategy());
15+
16+
print(strFormat);
17+
print(hexFormat);
18+
print(zipFormat);
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'view_strategy.dart';
2+
3+
class ByteContext {
4+
String toStringView(ViewStrategy strategy) {
5+
return '${strategy.runtimeType}:\n'
6+
'${strategy.out(this)}';
7+
}
8+
9+
void add(dynamic value) {
10+
_buf.add(value);
11+
}
12+
13+
List toList() => _buf;
14+
15+
final _buf = <dynamic>[];
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import 'byte_context.dart';
2+
3+
abstract class ViewStrategy {
4+
String out(ByteContext byteList);
5+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import 'dart:typed_data';
2+
3+
import '../pattern/byte_context.dart';
4+
import '../pattern/view_strategy.dart';
5+
6+
class HexViewStrategy implements ViewStrategy {
7+
@override
8+
String out(ByteContext byteList) {
9+
final buf = StringBuffer();
10+
11+
for (final val in byteList.toList()) {
12+
buf.write('${val.toString().padRight(15)}: ');
13+
14+
if (val is String) {
15+
buf.writeln(_stringToHex(val));
16+
} else {
17+
buf.writeln(_valueToHex(val, size: 8));
18+
}
19+
}
20+
21+
return buf.toString();
22+
}
23+
24+
String _stringToHex(String value) {
25+
return value.codeUnits
26+
.map((charCode) => _valueToHex(size: 1, charCode))
27+
.join(' ');
28+
}
29+
30+
String _valueToHex<T>(T value, {required int size}) {
31+
late ByteData byteData;
32+
33+
if (size == 1) {
34+
byteData = ByteData(1)..setInt8(0, value as int);
35+
} else {
36+
byteData = ByteData(size);
37+
if (value is double) {
38+
byteData.setFloat64(0, value);
39+
} else if (value is int) {
40+
byteData.setInt64(0, value);
41+
}
42+
}
43+
44+
final bytes = byteData.buffer.asUint8List();
45+
return bytes.map((e) => e.toRadixString(16).padLeft(2, '0')).join(' ');
46+
}
47+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import '../pattern/byte_context.dart';
2+
import '../pattern/view_strategy.dart';
3+
4+
class StrViewStrategy implements ViewStrategy {
5+
@override
6+
String out(ByteContext byteList) {
7+
return '${byteList.toList().join(', ')}\n';
8+
}
9+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import 'dart:io';
2+
3+
import '../pattern/byte_context.dart';
4+
import 'str_view_strategy.dart';
5+
6+
class ZipViewStrategy extends StrViewStrategy {
7+
@override
8+
String out(ByteContext byteList) {
9+
final codes = super.out(byteList).codeUnits;
10+
final bytes = GZipCodec().encode(codes);
11+
final buf = StringBuffer();
12+
13+
var odd = 1;
14+
for (final byte in bytes) {
15+
final hexByte = byte.toRadixString(16).padLeft(2, '0');
16+
buf.write('$hexByte ');
17+
18+
if (odd++ % 16 == 0) {
19+
buf.writeln();
20+
}
21+
}
22+
23+
return buf.toString();
24+
}
25+
}

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.29.0
3+
version: 0.30.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)