Determined users can still pass null to the constructor, no? We're
currently throwing an exception if the singleton exists and its
constructor is invoked, but I suppose this approach is a little cleaner.
-- 
Maciek Sakrejda
Truviso, Inc.
http://www.truviso.com

-----Original Message-----
From: m.ar80 <[EMAIL PROTECTED]>
Reply-To: [email protected]
To: [email protected]
Subject: [flexcoders] Re: private constructor in action script
Date: Thu, 07 Feb 2008 05:41:03 -0000

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