Created
November 4, 2023 17:02
dart singletons
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
// fancy constructor way | |
class SingletonOne { | |
SingletonOne._privateConstructor(); | |
static final SingletonOne _instance = SingletonOne._privateConstructor(); | |
factory SingletonOne() { | |
return _instance; | |
} | |
} | |
// static field with getter | |
class SingletonTwo { | |
SingletonTwo._privateConstructor(); | |
static final SingletonTwo _instance = SingletonTwo._privateConstructor(); | |
static SingletonTwo get instance => _instance; | |
} | |
// static field | |
class SingletonThree { | |
SingletonThree._privateConstructor(); | |
static final SingletonThree instance = SingletonThree._privateConstructor(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment