Private constructors in ActionScript
-allow-private-constructors
Apache Royale adds support for declaring the constructor of a class private
instead of public
in ActionScript. When a constructor is private, it cannot be instantiated with the new
keyword outside of the class where it is defined. Private constructors are commonly used for implementing the singleton design pattern, which is when only one instance of a particular class should ever be created.
Compiler option
Royale enables private constructors by default. To disable private constructors in your application, use the -allow-private-constructors
compiler option.
mxmlc -allow-private-constructors=false MyApp.mxml
Code example
You might implement a global logger as a singleton by using a private constructor.
package
{
public class Logger
{
private static var _instance:Logger;
public static function getInstance():Logger
{
if(!_instance)
{
_instance = new Logger();
}
return _instance;
}
private function Logger()
{
}
public function log(message:String):void
{
trace("LOG: " + message);
}
}
}
If you were to attempt to instantate this Logger
class, the compiler would fail with an error:
package com.example
{
public class MyApplication
{
public function MyApplication()
{
// Error: Attempted access of inaccessible constructor through a reference with static type Logger
var obj:Logger = new Logger();
}
}
}
However, you may call Logger.getInstance()
to access an instance that is created inside the Logger
class itself:
package com.example
{
public class MyApplication
{
public function MyApplication()
{
Logger.getInstance().log("Application started");
}
}
}
Limitations of private constructors in Royale
Checking whether a constructor is private happens at compile-time only. However, by using reflection APIs, a developer could gain access to a private constructor and instantiate it at run-time without errors.
If a SWC library contains classes with private constructors, applications using that library must also enable private constructors before the compiler will enforce any restrictions.
Other ActionScript compilers, such as the one in the Apache Flex SDK, may not recognize or enforce private constructors. Attemping to pass source code or SWC libraries that contain classes with private constructors to another compiler may result in compile-time errors or unexpected behavior at run-time. In other words, to write 100% portable ActionScript code that works with any compiler, you should avoid using private constructors and any of Royale’s other extensions to the ActionScript language.