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.

Revisions

  1. @theburningmonk theburningmonk renamed this gist Sep 2, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @theburningmonk theburningmonk revised this gist Sep 1, 2013. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion singleton
    Original file line number Diff line number Diff line change
    @@ -10,4 +10,10 @@ class MyClass {
    }

    ... // rest of the class
    }
    }

    // consuming code
    MyClass myObj = new MyClass(); // get back the singleton
    ...
    // another piece of consuming code
    MyClass myObj = new MyClass(); // still getting back the singleton
  3. @theburningmonk theburningmonk revised this gist Aug 31, 2013. 1 changed file with 5 additions and 10 deletions.
    15 changes: 5 additions & 10 deletions singleton
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,12 @@
    class DialogWindow extends Sprite {
    ResourceManager _resourceManager;
    class MyClass {
    static final MyClass _singleton = new MyClass._internal();

    static DialogWindow Singleton;

    // factory constructor to ensure that there is always only one instance of DialogWindow
    factory DialogWindow(resourceManager) {
    return Singleton != null ? Singleton : new DialogWindow._internal(resourceManager);
    factory MyClass() {
    return _singleton;
    }

    DialogWindow._internal(this._resourceManager) {
    MyClass._internal() {
    ... // initialization logic here

    Singleton = this;
    }

    ... // rest of the class
  4. @theburningmonk theburningmonk revised this gist Aug 31, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions singleton
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,17 @@
    class DialogWindow extends Sprite {
    ResourceManager _resourceManager;

    static DialogWindow Instance;
    static DialogWindow Singleton;

    // factory constructor to ensure that there is always only one instance of DialogWindow
    factory DialogWindow(resourceManager) {
    return Instance != null ? Instance : new DialogWindow._internal(resourceManager);
    return Singleton != null ? Singleton : new DialogWindow._internal(resourceManager);
    }

    DialogWindow._internal(this._resourceManager) {
    ... // initialization logic here

    Instance = this;
    Singleton = this;
    }

    ... // rest of the class
  5. @theburningmonk theburningmonk created this gist Aug 31, 2013.
    18 changes: 18 additions & 0 deletions singleton
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    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
    }