Haxe Code Cookbook
Haxe programming cookbookDesign patternsSingleton

Singleton

Reading time: 0.5 minute

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

class MySingleton {  
  // read-only property
  public static final instance: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:
Richard Oliver Bray
Timur
Mark Knol
Last modified:
Created:
Category:  Design patterns