Created
October 6, 2019 15:44
-
-
Save iapicca/c465a3254c80ef7311493a76b24b643f to your computer and use it in GitHub Desktop.
Simple Singleton
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
class Singleton { | |
int _counter; | |
static final Singleton _instance = Singleton._internal(); | |
factory Singleton()=> _instance; | |
Singleton._internal() { | |
_counter = 0; | |
} | |
void counterUp() => _counter +=1; | |
String get counter => _counter.toString(); | |
} | |
void main() { | |
Singleton _first = Singleton(); | |
Singleton _second = Singleton(); | |
for(int i=0;i < 5;i++){ | |
_first.counterUp(); | |
print(_second.counter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment