Haxe Code Cookbook
Haxe programming cookbookDesign patternsSingleton

Singleton

This is a basic example of the Singleton design pattern in Haxe.

class MySingleton {
  // read-only property
  public static var instance(default, null):MySingleton = new MySingleton();
  
  private function new () {}  // private constructor
}

Usage

class Main {
  public static function main () {
    // this will be the only way to access the instance
    MySingleton.instance;

    // This will throw error "Cannot access private constructor"
    // new MySingleton(); 
  }
}

Contributors:
Last modified:
Created:
Category:  Design patterns