Created
June 23, 2019 00:45
-
-
Save marcobraghim/b6590f19d14c8efc854dfa323f507f44 to your computer and use it in GitHub Desktop.
Singleton with Dart lang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
Singleton singleton1 = new Singleton(); | |
Singleton singleton2 = new Singleton(); | |
print(identical(singleton1, singleton2)); | |
print(TrueSingleton.getInstance()); | |
} | |
class Singleton { | |
static final Singleton _singleton = new Singleton._internal(); | |
factory Singleton() { | |
return _singleton; | |
} | |
Singleton._internal(); | |
} | |
class TrueSingleton { | |
static final TrueSingleton _singleton = new TrueSingleton._internal(); | |
TrueSingleton._internal(); | |
static TrueSingleton getInstance() => _singleton; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment