Last active
November 22, 2019 14:08
-
-
Save luizjacomn/3f0dde84751cf37a2bf972afd8a3d786 to your computer and use it in GitHub Desktop.
Implementando o padrão Singleton em dart. Baseado em https://forum.flutterando.com.br/d/59-criando-singleton-com-dart-flutter
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() { | |
Contador single = Contador(); | |
Contador single2 = Contador(); | |
Contador single3 = Contador(); | |
print(single.count++); | |
print(single2.count++); | |
print(single3.count); | |
} | |
class Contador { | |
// Variável qualquer | |
int count = 0; | |
// Variável que representa a instância única | |
static Contador _instancia; | |
factory Contador() { | |
// Se a _instancia não foi criada ainda (null), chama o _construtorInterno() | |
_instancia ??= Contador._construtorInterno(); | |
return _instancia; | |
} | |
// Devolve uma instancia ca classe Contador | |
Contador._construtorInterno(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment