-
Notifications
You must be signed in to change notification settings - Fork 2
Singleton
Manno edited this page Sep 8, 2016
·
4 revisions
The Singleton pattern is very well explained at Robert Nystrom's Game Programming Patterns.
This implementation follows the Haxe Code Cookbook example using Haxe properties instead of variables. Main difference between variables and properties is that properties allow for more access control than the private/public predicates alone can provide.
In this example the variable called instance is read-only. This is enforced by the use of the (default, null) directly after the variable name. 'default' means the variable can be read from outside the class and 'null' means the variable cannot be set/written from outside the class. In essence it is a public variable with read only access.
// in the singleton class:
public static var instance(default, null):SingletonDemo = new SingletonDemo();
// how to get access to the instance variable from outside the class:
SingletonDemo.instance