Skip to content

Commit fb18f70

Browse files
authored
Merge pull request #40 from ilopX/add-abstract-factory-pattern
Add Conceptual Gui Factory - abstract factory pattern.
2 parents 3aa4495 + 6a38ace commit fb18f70

File tree

15 files changed

+153
-2
lines changed

15 files changed

+153
-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.19.0
2+
- Add "Conceptual Gui Factory" example.
3+
14
## 0.18.0
25
- Add Memento Editor.
36

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ It contains **Dart** examples for all classic **GoF** design patterns.
44

55
# Implementation checklist:
66
- [ ] **Creation**
7-
- [ ] **Abstract Factory**
7+
- [ ] **Abstract Factory** [[Conceptual Gui Factory](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/abstract_factory/conceptual_gui_factory)]
88
- [x] **Builder** - [[Color Text Format](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/builder/color_text_format)]
99
- [ ] **Factory Method**
1010
- [x] **Prototype** - [[Shapes](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/prototype/shapes)]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
### About example.
8+
This the very conceptual example rewrite from original source code [java example](https://github.com/RefactoringGuru/design-patterns-java/tree/main/src/refactoring_guru/abstract_factory/example)
9+
10+
### Diagram:
11+
![image](https://user-images.githubusercontent.com/8049534/165987890-e64db9a3-4865-411c-a5c0-16da21043159.png)
12+
13+
### Client code:
14+
```dart
15+
void main() {
16+
final guiFactory = GUIFactory();
17+
final app = Application(guiFactory);
18+
app.paint();
19+
}
20+
21+
abstract class GUIFactory {
22+
factory GUIFactory() {
23+
if (Platform.isMacOS) {
24+
return MacOSFactory();
25+
} else {
26+
return WindowsFactory();
27+
}
28+
}
29+
30+
/*...*/
31+
}
32+
```
33+
34+
### Output:
35+
```
36+
You have created WindowsButton.
37+
You have created WindowsCheckbox.
38+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import '../button/button.dart';
2+
import '../checkbox/checkbox.dart';
3+
import '../factories/gui_factory.dart';
4+
5+
class Application {
6+
late Button _button;
7+
late Checkbox _checkbox;
8+
9+
Application(GUIFactory factory) {
10+
_button = factory.createButton();
11+
_checkbox = factory.createCheckbox();
12+
}
13+
14+
void paint() {
15+
_button.paint();
16+
_checkbox.paint();
17+
}
18+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
abstract class Button {
2+
void paint();
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import 'button.dart';
2+
3+
class MacOSButton implements Button {
4+
@override
5+
void paint() {
6+
print('You have created MacOSButton.');
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import 'button.dart';
2+
3+
class WindowsButton implements Button {
4+
@override
5+
void paint() {
6+
print('You have created WindowsButton.');
7+
}
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
abstract class Checkbox {
2+
void paint();
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import 'checkbox.dart';
2+
3+
class MacOSCheckbox implements Checkbox {
4+
@override
5+
void paint() {
6+
print('You have created MacOSCheckbox.');
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import 'checkbox.dart';
2+
3+
class WindowsCheckbox implements Checkbox {
4+
@override
5+
void paint() {
6+
print('You have created WindowsCheckbox.');
7+
}
8+
}

0 commit comments

Comments
 (0)