i don't know about best practices but it's quite easy to implement
package test
{
import flash.utils.Dictionary;
public class SingletonManager
{
private static const instances:Dictionary = new Dictionary;
public static function getInstance(clazz:Class):Object
{
if (!instances[clazz])
{
instances[clazz] = new clazz;
}
return instances[clazz];
}
}
}
Also you could take a look at slightly complex implementation in Flex
- mx.core.Singleton
Also you would probably need to force users of your class not to use
public constructor.
It could be easy done with following (extract)
private static var iExists:Boolean;
public function MyClass()
{
if (iExists)
{
throw new Error("My only instance already exists!");
}
iExists = true;
}
Regards,
Andrii Olefirenko
--- In [email protected], "Dominic Pazula" <[EMAIL PROTECTED]> wrote:
>
> I'm attempting to write singleton manager for my application. I've
> seen numerous ways for this to be done. I traced through how the
> BrowserManager and the PopUpManager are implemented. (For the record,
> I tried, and failed, to copy how BrowserManager is implemented).
>
> Does anyone know what the best practices are on creating this type of
> class? Any gotchas to look out for?
>
> Thanks
> Dominic
>