As of now, it is not possible to declare a constructor private (other
than public) in ActionScript 3.0. However, as pointed out, it is
possible to achieve the required behavior by using a dummy private
class defined outside the package so that no one has access to it
outside the file. Passing a dummy instance of this class to the
constructor of the singleton class should prevent other users from
instantiating the singleton class. Here is a code snippet illustrating
the concept:

package
{
        class Singleton
        {
                private static var theOneAndOnlyInstance:Singleton;

                public function Singleton(dummy:DummyPrivateClass)
                {
                        super();
                }
                
                public static function getInstance():Singleton
                {
                        if(theOneAndOnlyInstance == null)
                        {
                                theOneAndOnlyInstance = new Singleton(new 
DummyPrivateClass());
                        }
                        return theOneAndOnlyInstance;
                }
        }
}

class DummyPrivateClass
{
        public function DummyPrivateClass() { super(); }
}

HTH.

Reply via email to