Skip to content

Commit 1edebd8

Browse files
authored
Merge pull request #66 from ilopX/add-conceptual-flyweight-pattern
Add conceptual flyweight pattern
2 parents f3b2ce7 + 26c5bc4 commit 1edebd8

File tree

9 files changed

+133
-2
lines changed

9 files changed

+133
-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.28.0
2+
- Add conceptual flyweight pattern.
3+
14
## 0.27.0
25
- Add template method pattern: Data Miner.
36

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ It contains **Dart** examples for all classic **GoF** design patterns.
2727
- [x] **Composite** - [[Image Editor](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/composite/image_editor)] [[Products and Boxes](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/composite/products_and_boxes)]
2828
- [x] **Decorator** - [[Data Source Decoder](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/decorator/data_source_decoder)]
2929
- [ ] **Facade**
30-
- [ ] **Flyweight**
30+
- [x] **Flyweight** [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/flyweight/conceptual)]
3131
- [ ] **Proxy**
3232

3333
## Requirements
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Flyweight Pattern
2+
Flyweight is a structural design pattern that lets you fit more objects into the available amount of
3+
RAM by sharing common parts of state between multiple objects instead of keeping all of the data in
4+
each object.
5+
6+
Tutorial: [here](https://refactoring.guru/design-patterns/flyweight).
7+
8+
### Conceptual Diagram:
9+
![Flyweight Pattern Diagram](https://user-images.githubusercontent.com/8049534/174476638-007f3179-495f-499d-8f35-e33956d4890b.png)
10+
11+
### Client code:
12+
```dart
13+
void main() {
14+
final factory = FlyweightFactory();
15+
16+
final one = factory.create('one');
17+
one.draw();
18+
19+
final ONE = factory.create('ONE');
20+
ONE.draw();
21+
22+
final OnE = factory.create('OnE');
23+
OnE.draw();
24+
25+
final two = factory.create('two');
26+
two.draw();
27+
28+
final Two = factory.create('Two');
29+
Two.draw();
30+
}
31+
```
32+
33+
### Output:
34+
```
35+
Flyweight(
36+
localParam: "one", shareParam: (61, "b", 0.19)
37+
)
38+
Flyweight(
39+
localParam: "ONE", shareParam: (61, "b", 0.19)
40+
)
41+
Flyweight(
42+
localParam: "OnE", shareParam: (61, "b", 0.19)
43+
)
44+
Flyweight(
45+
localParam: "two", shareParam: (53, "e", 0.55)
46+
)
47+
Flyweight(
48+
localParam: "Two", shareParam: (53, "e", 0.55)
49+
)
50+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ignore_for_file: non_constant_identifier_names
2+
3+
import 'pattern/flyweight_factory.dart';
4+
5+
void main() {
6+
final factory = FlyweightFactory();
7+
8+
final one = factory.create('one');
9+
one.draw();
10+
11+
final ONE = factory.create('ONE');
12+
ONE.draw();
13+
14+
final OnE = factory.create('OnE');
15+
OnE.draw();
16+
17+
final two = factory.create('two');
18+
two.draw();
19+
20+
final Two = factory.create('Two');
21+
Two.draw();
22+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class ShareParams {
2+
final int param1;
3+
final String param2;
4+
final double param3;
5+
6+
ShareParams(this.param1, this.param2, this.param3);
7+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import '../params/share_params.dart';
2+
3+
class Flyweight {
4+
final String localParam;
5+
final ShareParams shareParams;
6+
7+
Flyweight(this.localParam, this.shareParams);
8+
9+
void draw() {
10+
print('Flyweight(');
11+
print('\tlocalParam: "$localParam", shareParam: ('
12+
'${shareParams.param1}, '
13+
'"${shareParams.param2}", '
14+
'${shareParams.param3})');
15+
print(')');
16+
}
17+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import 'flyweight.dart';
2+
import '../params/share_params.dart';
3+
import '../utils/fake_value.dart';
4+
5+
class FlyweightFactory {
6+
Flyweight create(String localParam) {
7+
final shareParams = _shares.putIfAbsent(
8+
localParam.toLowerCase(),
9+
() => ShareParams(
10+
fakeInt,
11+
fakeString,
12+
fakeDouble,
13+
),
14+
);
15+
16+
return Flyweight(localParam, shareParams);
17+
}
18+
19+
final _shares = <String, ShareParams>{};
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'dart:math';
2+
3+
String str = 'abcdef';
4+
get fakeString {
5+
final char = str[Random().nextInt(str.length - 1)];
6+
str = str.replaceFirst(char, '');
7+
return char;
8+
}
9+
10+
get fakeInt => Random().nextInt(100);
11+
12+
get fakeDouble => Random().nextInt(100) / 100;

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.27.0
3+
version: 0.28.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)