File tree 6 files changed +66
-2
lines changed
patterns/singleton/conceptual
6 files changed +66
-2
lines changed Original file line number Diff line number Diff line change
1
+ ## 0.34.0
2
+ - Add conceptual single pattern.
3
+
1
4
## 0.33.0
2
5
- Add conceptual builder pattern.
3
6
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ It contains **Dart** examples for all classic **GoF** design patterns.
8
8
- [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 )]
9
9
- [x] ** Factory Method** [[ Conceptual Platform Dialog] ( https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/factory_method/conceptual_platform_dialog )]
10
10
- [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 )]
12
12
- [ ] ** Behavioral**
13
13
- [x] ** Chain of Responsibility** - [[ Server Middleware] ( https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/chain_of_responsibility/server_middleware )]
14
14
- [x] ** Command** - [[ Text Editor] ( https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/command/text_editor )]
Original file line number Diff line number Diff line change
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
+ ```
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
1
name : design_patterns_dart
2
2
description : Dart examples for all classic GoF design patterns.
3
- version : 0.33 .0
3
+ version : 0.34 .0
4
4
homepage : https://refactoring.guru/design-patterns
5
5
repository : https://github.com/RefactoringGuru/design-patterns-dart
6
6
issue_tracker : https://github.com/RefactoringGuru/design-patterns-dart/issue
You can’t perform that action at this time.
0 commit comments