File tree Expand file tree Collapse file tree 6 files changed +66
-2
lines changed
patterns/singleton/conceptual Expand file tree Collapse file tree 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+
14## 0.33.0
25- Add conceptual builder pattern.
36
Original file line number Diff line number Diff 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 )]
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 11name : design_patterns_dart
22description : Dart examples for all classic GoF design patterns.
3- version : 0.33 .0
3+ version : 0.34 .0
44homepage : https://refactoring.guru/design-patterns
55repository : https://github.com/RefactoringGuru/design-patterns-dart
66issue_tracker : https://github.com/RefactoringGuru/design-patterns-dart/issue
You can’t perform that action at this time.
0 commit comments