Skip to content

Add singleton pattern. #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.34.0
- Add conceptual single pattern.

## 0.33.0
- Add conceptual builder pattern.

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ It contains **Dart** examples for all classic **GoF** design patterns.
- [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)]
- [x] **Factory Method** [[Conceptual Platform Dialog](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/factory_method/conceptual_platform_dialog)]
- [x] **Prototype** - [[Shapes](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/prototype/shapes)]
- [ ] **Singleton**
- [x] **Singleton** - [[Conceptual](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/singleton/conceptual)]
- [ ] **Behavioral**
- [x] **Chain of Responsibility** - [[Server Middleware](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/chain_of_responsibility/server_middleware)]
- [x] **Command** - [[Text Editor](https://github.com/RefactoringGuru/design-patterns-dart/tree/main/patterns/command/text_editor)]
Expand Down
28 changes: 28 additions & 0 deletions patterns/singleton/conceptual/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Singleton Pattern
Singleton is a creational design pattern that lets you ensure that a class has only one instance,
while providing a global access point to this instance.

Tutorial: [here](https://refactoring.guru/design-patterns/singleton).

### Diagram:
![Singleton Pattern Diagram](https://user-images.githubusercontent.com/8049534/182938119-78a21534-5751-4dea-afa3-8acaec46eed9.png)

### Client code:
```dart
void main() {
// dart style
Singleton().doSome();
Singleton().doSome();

// standard style
Singleton.instance.doSome();
}
```

### Output:
```
Create singleton once.
doSome()
doSome()
doSome()
```
10 changes: 10 additions & 0 deletions patterns/singleton/conceptual/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'pattern/singleton.dart';

void main() {
// dart style
Singleton().doSome();
Singleton().doSome();

// standard style
Singleton.instance.doSome();
}
23 changes: 23 additions & 0 deletions patterns/singleton/conceptual/pattern/singleton.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
abstract class Singleton {
factory Singleton() {
if (_instance == null) {
print('Create singleton once.');
_instance = ConcreteSingleton();
}

return _instance!;
}

static Singleton get instance => Singleton();

void doSome();

static Singleton? _instance;
}

class ConcreteSingleton implements Singleton {
@override
void doSome() {
print('doSome()');
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: design_patterns_dart
description: Dart examples for all classic GoF design patterns.
version: 0.33.0
version: 0.34.0
homepage: https://refactoring.guru/design-patterns
repository: https://github.com/RefactoringGuru/design-patterns-dart
issue_tracker: https://github.com/RefactoringGuru/design-patterns-dart/issue
Expand Down