Skip to content

Commit 09a1aa0

Browse files
committed
Add README.
1 parent 8c33418 commit 09a1aa0

File tree

1 file changed

+49
-0
lines changed
  • patterns/abstract_factory/tool_panel_factory

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Abstract Factory Pattern
2+
Abstract Factory is a creational design pattern that lets you produce families of related objects
3+
without specifying their concrete classes.
4+
5+
Tutorial: [here](https://refactoring.guru/design-patterns/abstract-factory).
6+
7+
### Online demo:
8+
Click on the picture to see the [demo](https://RefactoringGuru.github.io/design-patterns-dart/#/abstract_factory/tool_panel_factory).
9+
10+
[![image](https://user-images.githubusercontent.com/8049534/168668992-369a1bab-9f97-4333-a20e-ffd06bf91b54.png)](https://refactoringguru.github.io/design-patterns-dart/#/abstract_factory/tool_panel_factory)
11+
12+
### Diagram:
13+
![image](https://user-images.githubusercontent.com/8049534/168672053-73ae1c9c-8fad-45ae-9247-429f7b5da565.png)
14+
15+
### Client code (using the "createShape" method):
16+
```dart
17+
final app = App(
18+
tools: Tools(
19+
factories: [
20+
TxtFactory(),
21+
LineFactory(),
22+
CircleFactory(),
23+
TriangleFactory(),
24+
StarFactory(),
25+
],
26+
),
27+
);
28+
29+
class App {
30+
void addShape(double x, double y) {
31+
final newShape = activeFactory.createShape(x, y, activeColor);
32+
shapes.add(newShape);
33+
}
34+
}
35+
36+
mixin IconBoxMixin implements FactoryTool {
37+
Image? _icon;
38+
39+
@override
40+
Image get icon => _icon!;
41+
42+
Future<void> updateIcon(Color color) async {
43+
final shape = createShape(0, 0, color);
44+
final pngBytes = await _pngImageFromShape(shape);
45+
_icon = Image.memory(pngBytes);
46+
}
47+
}
48+
```
49+

0 commit comments

Comments
 (0)