Skip to content

Instantly share code, notes, and snippets.

@loivis
Forked from theburningmonk/singleton.dart
Created April 20, 2019 15:45
Show Gist options
  • Select an option

  • Save loivis/c7e17f9ed5928f346770863fde1eacdc to your computer and use it in GitHub Desktop.

Select an option

Save loivis/c7e17f9ed5928f346770863fde1eacdc to your computer and use it in GitHub Desktop.
Using Dart's factory constructor feature to implement the singleton pattern
class DialogWindow extends Sprite {
ResourceManager _resourceManager;
static DialogWindow Instance;
// factory constructor to ensure that there is always only one instance of DialogWindow
factory DialogWindow(resourceManager) {
return Instance != null ? Instance : new DialogWindow._internal(resourceManager);
}
DialogWindow._internal(this._resourceManager) {
... // initialization logic here
Instance = this;
}
... // rest of the class
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment