Skip to content

Commit 2388dda

Browse files
authored
Merge pull request #44 from ilopX/add-pattern-strategy
Add strategy pattern: "Reservation cargo spaces".
2 parents 9d246c7 + 59a6e91 commit 2388dda

File tree

10 files changed

+125
-2
lines changed

10 files changed

+125
-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.21.0
2+
- Add strategy pattern: "Reservation cargo spaces".
3+
14
## 0.20.0
25
- Add "Conceptual Dialog Factory" example.
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
- [ ] **State**
2121
- [ ] **Template Method**
2222
- [ ] **Visitor**
23-
- [ ] **Strategy**
23+
- [ ] **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)] [[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+
# Memento 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+
## About example: Reservation cargo spaces.
8+
This example is taken from the "Blue" book **"Domain-Driven Design" - Eric Evans. Chapter Once**.
9+
10+
### Diagram:
11+
![image](https://user-images.githubusercontent.com/8049534/166560051-6e392b01-6777-4eb1-ae20-fcd643b248ef.png)
12+
13+
### Client code:
14+
```dart
15+
void main() {
16+
final overbookingPolicy = OverbookingPolicy();
17+
final app = Application(overbookingPolicy);
18+
final voyage = Voyage();
19+
20+
try {
21+
app.makeBooking(Cargo(1000), voyage);
22+
app.makeBooking(Cargo(500), voyage);
23+
app.makeBooking(Cargo(800), voyage); // error
24+
} catch (e) {
25+
print(e);
26+
}
27+
}
28+
29+
class Application {
30+
void makeBooking(Cargo cargo, Voyage voyage) {
31+
if (overbookingPolicy.isAllowed(cargo, voyage)) {
32+
voyage.addCargo(cargo, confirmation);
33+
}
34+
}
35+
}
36+
```
37+
38+
**Output:**
39+
```
40+
add Cargo(1000.0) to voyage.
41+
add Cargo(500.0) to voyage.
42+
The weight of the cargo exceeds the permissible norm.
43+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import '../partners/cargo.dart';
2+
import '../partners/voyage.dart';
3+
import '../policy/overbooking_policy.dart';
4+
import 'order_confirmation_sequence.dart';
5+
6+
class Application {
7+
final OverbookingPolicy overbookingPolicy;
8+
final orderConfirmationSequence = OrderConfirmationSequence();
9+
10+
Application(this.overbookingPolicy);
11+
12+
void makeBooking(Cargo cargo, Voyage voyage) {
13+
if (overbookingPolicy.isAllowed(cargo, voyage)) {
14+
final confirmation = orderConfirmationSequence.next();
15+
voyage.addCargo(cargo, confirmation);
16+
} else {
17+
throw 'The weight of the cargo exceeds the permissible norm.';
18+
}
19+
}
20+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class OrderConfirmationSequence {
2+
var _index = 0;
3+
4+
int next() => _index++;
5+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'application/application.dart';
2+
import 'partners/cargo.dart';
3+
import 'partners/voyage.dart';
4+
import 'policy/overbooking_policy.dart';
5+
6+
void main() {
7+
final overbookingPolicy = OverbookingPolicy();
8+
final app = Application(overbookingPolicy);
9+
final voyage = Voyage();
10+
11+
try {
12+
app.makeBooking(Cargo(1000), voyage);
13+
app.makeBooking(Cargo(500), voyage);
14+
app.makeBooking(Cargo(800), voyage); // error
15+
} catch (e) {
16+
print(e);
17+
}
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Cargo {
2+
final double size;
3+
4+
Cargo(this.size);
5+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'cargo.dart';
2+
3+
class Voyage {
4+
final _cargo = <int, Cargo>{};
5+
6+
double get capacity => 2000.0;
7+
8+
double bookedCargoSize() {
9+
return _cargo.values.fold(0, (prev, cargo) => prev + cargo.size);
10+
}
11+
12+
void addCargo(Cargo cargo, int confirmation) {
13+
_cargo.putIfAbsent(confirmation, () => cargo);
14+
print('Add Cargo(${cargo.size}) to voyage.');
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import '../partners/cargo.dart';
2+
import '../partners/voyage.dart';
3+
4+
class OverbookingPolicy {
5+
static const allowableRedundancy = 1.1;
6+
7+
bool isAllowed(Cargo cargo, Voyage voyage) {
8+
final maxBooking = voyage.capacity * allowableRedundancy;
9+
final futureWeight = voyage.bookedCargoSize() + cargo.size;
10+
11+
return futureWeight < maxBooking;
12+
}
13+
}

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.20.0
3+
version: 0.21.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)