Singleton Class in Flex
actionscript, apache-flex
Solution
Can of worms asking about singletons!
There are a few different options about creating singletons mainly due to AS3 not having private constructors. Here's the pattern we use.
package com.foo.bar {
public class Blah {
private static var instance : Blah;
public function Blah( enforcer : SingletonEnforcer ) {}
public static function getInstance() : Blah {
if (!instance) {
instance = new Blah( new SingletonEnforcer() );
}
return instance;
}
...
}
}
class SingletonEnforcer{}
Note that the SingletonEnforcer class is internal so can only be used by the Blah class (effectively). No-one can directly instantiate the class, they have to go through the getInstance() function.
Problem
I have a doubt,.... How would you create a Singleton class in Flex... Is there any convention like the class name should eb Singleton or it should extend any other class. How many Singleton class can a project have? Can anyone say the real time usage of a Singleton class? I am planning to keep my components label texts in a Singleton class... Is it a good approach.