Skip to content

Instantly share code, notes, and snippets.

@marcobraghim
Created June 23, 2019 00:45
Show Gist options
  • Save marcobraghim/b6590f19d14c8efc854dfa323f507f44 to your computer and use it in GitHub Desktop.
Save marcobraghim/b6590f19d14c8efc854dfa323f507f44 to your computer and use it in GitHub Desktop.
Singleton with Dart lang
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