SINGLETON in as3

To implement the pattern singleton in action script we quickly collide with a limitation of the language: we cannot return a private constructor and it is the base of the pattern.
By looking on internet we find several more or less evolved solutions which allow to protect the usage of the constructor.Often an exception is raised if it is not called by the static getInstance method.
The inconvenience of this approach is that the error is raise only during runtime and not in the compilation. In the book « ActionScript 3.0 Design Patterns » the author proposes another approach.

The principle is to use a private class described in our Singleton class but in the outside of the package:


package
{
public class Singleton
{
private static var _instance:Singleton;
public function Singleton(pvt:PrivateClass){
}
public static function getInstance():Singleton
{
if(Singleton._instance==null){
Singleton._instance=new Singleton(new PrivateClass()};
}
return Singleton._instance;
}
}
}
//END OF PACKAGE
//Private class
class PrivateClass
{
public function PrivateClass(){}
}

The advantage is that the private class used in the constructor is visible only in the Singleton class because it is defines outside the package.
Indeed the code following in another class raise an error after compilation.

Singleton s=new Singleton(new PrivateClass())};

Here is an elegant way to implement this pattern.