Last active
May 5, 2019 18:43
-
-
Save googlicius/91d699ceda3fc7ca8e5b6d9c29457171 to your computer and use it in GitHub Desktop.
Dart factory
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 Logger { | |
final String name; | |
bool mute = false; | |
static final Map<String, Logger> _cache = <String, Logger>{}; | |
factory Logger(String name) { | |
if(_cache.containsKey(name)) { | |
return _cache[name]; | |
} | |
else { | |
final logger = Logger._internal(name); | |
_cache[name] = logger; | |
return logger; | |
} | |
} | |
Logger._internal(this.name); | |
void log(String msg) { | |
if(!mute) { | |
print(msg); | |
} | |
} | |
} | |
main() { | |
var logger = Logger('UI'); | |
logger.log('Button clicked'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment