Skip to content

Add strategy pattern: "Reservation cargo spaces". #44

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 4 commits into from
May 3, 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.21.0
- Add strategy pattern: "Reservation cargo spaces".

## 0.20.0
- Add "Conceptual Dialog Factory" example.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ It contains **Dart** examples for all classic **GoF** design patterns.
- [ ] **State**
- [ ] **Template Method**
- [ ] **Visitor**
- [ ] **Strategy**
- [ ] **Strategy** [[Reservation cargo spaces](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/strategy/reservation_cargo_spaces)]
- [ ] **Structural**
- [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)]
- [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)]
Expand Down
43 changes: 43 additions & 0 deletions patterns/strategy/reservation_cargo_spaces/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Memento pattern
Strategy is a behavioral design pattern that lets you define a family of algorithms, put each of
them into a separate class, and make their objects interchangeable.

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

## About example: Reservation cargo spaces.
This example is taken from the "Blue" book **"Domain-Driven Design" - Eric Evans. Chapter Once**.

### Diagram:
![image](https://user-images.githubusercontent.com/8049534/166560051-6e392b01-6777-4eb1-ae20-fcd643b248ef.png)

### Client code:
```dart
void main() {
final overbookingPolicy = OverbookingPolicy();
final app = Application(overbookingPolicy);
final voyage = Voyage();

try {
app.makeBooking(Cargo(1000), voyage);
app.makeBooking(Cargo(500), voyage);
app.makeBooking(Cargo(800), voyage); // error
} catch (e) {
print(e);
}
}

class Application {
void makeBooking(Cargo cargo, Voyage voyage) {
if (overbookingPolicy.isAllowed(cargo, voyage)) {
voyage.addCargo(cargo, confirmation);
}
}
}
```

**Output:**
```
add Cargo(1000.0) to voyage.
add Cargo(500.0) to voyage.
The weight of the cargo exceeds the permissible norm.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import '../partners/cargo.dart';
import '../partners/voyage.dart';
import '../policy/overbooking_policy.dart';
import 'order_confirmation_sequence.dart';

class Application {
final OverbookingPolicy overbookingPolicy;
final orderConfirmationSequence = OrderConfirmationSequence();

Application(this.overbookingPolicy);

void makeBooking(Cargo cargo, Voyage voyage) {
if (overbookingPolicy.isAllowed(cargo, voyage)) {
final confirmation = orderConfirmationSequence.next();
voyage.addCargo(cargo, confirmation);
} else {
throw 'The weight of the cargo exceeds the permissible norm.';
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class OrderConfirmationSequence {
var _index = 0;

int next() => _index++;
}
18 changes: 18 additions & 0 deletions patterns/strategy/reservation_cargo_spaces/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'application/application.dart';
import 'partners/cargo.dart';
import 'partners/voyage.dart';
import 'policy/overbooking_policy.dart';

void main() {
final overbookingPolicy = OverbookingPolicy();
final app = Application(overbookingPolicy);
final voyage = Voyage();

try {
app.makeBooking(Cargo(1000), voyage);
app.makeBooking(Cargo(500), voyage);
app.makeBooking(Cargo(800), voyage); // error
} catch (e) {
print(e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Cargo {
final double size;

Cargo(this.size);
}
16 changes: 16 additions & 0 deletions patterns/strategy/reservation_cargo_spaces/partners/voyage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'cargo.dart';

class Voyage {
final _cargo = <int, Cargo>{};

double get capacity => 2000.0;

double bookedCargoSize() {
return _cargo.values.fold(0, (prev, cargo) => prev + cargo.size);
}

void addCargo(Cargo cargo, int confirmation) {
_cargo.putIfAbsent(confirmation, () => cargo);
print('Add Cargo(${cargo.size}) to voyage.');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import '../partners/cargo.dart';
import '../partners/voyage.dart';

class OverbookingPolicy {
static const allowableRedundancy = 1.1;

bool isAllowed(Cargo cargo, Voyage voyage) {
final maxBooking = voyage.capacity * allowableRedundancy;
final futureWeight = voyage.bookedCargoSize() + cargo.size;

return futureWeight < maxBooking;
}
}
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.20.0
version: 0.21.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