Skip to content

Commit 4fcfede

Browse files
authored
Merge pull request #72 from ilopX/add-singleton-pattern
Add singleton pattern.
2 parents c46cc84 + d946967 commit 4fcfede

File tree

6 files changed

+66
-2
lines changed

6 files changed

+66
-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.34.0
2+
- Add conceptual single pattern.
3+
14
## 0.33.0
25
- Add conceptual builder pattern.
36

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ It contains **Dart** examples for all classic **GoF** design patterns.
88
- [x] **Builder** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/builder/conceptual)] [[Color Text Format](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/builder/color_text_format)]
99
- [x] **Factory Method** [[Conceptual Platform Dialog](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/factory_method/conceptual_platform_dialog)]
1010
- [x] **Prototype** - [[Shapes](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/prototype/shapes)]
11-
- [ ] **Singleton**
11+
- [x] **Singleton** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/singleton/conceptual)]
1212
- [ ] **Behavioral**
1313
- [x] **Chain of Responsibility** - [[Server Middleware](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/chain_of_responsibility/server_middleware)]
1414
- [x] **Command** - [[Text Editor](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/command/text_editor)]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Singleton Pattern
2+
Singleton is a creational design pattern that lets you ensure that a class has only one instance,
3+
while providing a global access point to this instance.
4+
5+
Tutorial: [here](https://refactoring.guru/design-patterns/singleton).
6+
7+
### Diagram:
8+
![Singleton Pattern Diagram](https://user-images.githubusercontent.com/8049534/182938119-78a21534-5751-4dea-afa3-8acaec46eed9.png)
9+
10+
### Client code:
11+
```dart
12+
void main() {
13+
// dart style
14+
Singleton().doSome();
15+
Singleton().doSome();
16+
17+
// standard style
18+
Singleton.instance.doSome();
19+
}
20+
```
21+
22+
### Output:
23+
```
24+
Create singleton once.
25+
doSome()
26+
doSome()
27+
doSome()
28+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import 'pattern/singleton.dart';
2+
3+
void main() {
4+
// dart style
5+
Singleton().doSome();
6+
Singleton().doSome();
7+
8+
// standard style
9+
Singleton.instance.doSome();
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
abstract class Singleton {
2+
factory Singleton() {
3+
if (_instance == null) {
4+
print('Create singleton once.');
5+
_instance = ConcreteSingleton();
6+
}
7+
8+
return _instance!;
9+
}
10+
11+
static Singleton get instance => Singleton();
12+
13+
void doSome();
14+
15+
static Singleton? _instance;
16+
}
17+
18+
class ConcreteSingleton implements Singleton {
19+
@override
20+
void doSome() {
21+
print('doSome()');
22+
}
23+
}

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.33.0
3+
version: 0.34.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)