Skip to content

Commit 9b62d27

Browse files
committed
Add README.
1 parent 3ef63dc commit 9b62d27

File tree

1 file changed

+43
-0
lines changed
  • patterns/strategy/reservation_cargo_spaces

1 file changed

+43
-0
lines changed
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+
```

0 commit comments

Comments
 (0)