[flexcoders] Re: Anyone have a good resource on singletons?
With regard to Troy's comment on "making it *impossible* to create multiple instances..." - I would add.. multiple instances shouldn't be a huge problem unless you are using modules in your app. If you are - it is quite important to make sure you watch that like a hawk. http://www.quilix.com/node/5 Rick Winscot On Mon, Jul 21, 2008 at 10:04 AM, Troy Gilbert <[EMAIL PROTECTED]> wrote: >>> I'm trying to find information on using singletons in AS3/Flex. I've >>> got an .AS file set up but I'm having issues calling the data/functions >>> within that function in other classes. Does anyone have a good resource >>> on the web for creating and using singletons? > > As others have probably pointed out, it sounds like you may be trying > to use a singleton to solve an architectural issue possibly not best > solved with a singleton. But, if singleton is appropriate... > > Don't make it more complicated than it needs to be. Everyone seems to > do that with singletons, which is unfortunate since singletons should > be so rarely used anyway. Just create a class that creates and returns > an instance on first access, then returns that instance on further > access. If you're worried about other developers constructing multiple > instances of your class, then just include a test in the constructor > and have it throw an error or assert. > > Don't worry about making it *impossible* to create multiple > instances... it's just not worth the effort. If people are determined > to use your classes incorrectly and disregard warnings and errors, > etc, then they're digging their own grave. > > Here's the implementation I use: > > public class Singleton { > > private static _instance:Singleton; > > public function get instance():Singleton { > if (_instance == null) _instance = new Singleton(); > return _instance; > } > > public function Singleton() { > if (_instance != null) throw new Error("This class is a singleton. > Instance already created."); > } > } > > Troy. > >
Re: [flexcoders] Re: Anyone have a good resource on singletons?
>> I'm trying to find information on using singletons in AS3/Flex. I've >> got an .AS file set up but I'm having issues calling the data/functions >> within that function in other classes. Does anyone have a good resource >> on the web for creating and using singletons? As others have probably pointed out, it sounds like you may be trying to use a singleton to solve an architectural issue possibly not best solved with a singleton. But, if singleton is appropriate... Don't make it more complicated than it needs to be. Everyone seems to do that with singletons, which is unfortunate since singletons should be so rarely used anyway. Just create a class that creates and returns an instance on first access, then returns that instance on further access. If you're worried about other developers constructing multiple instances of your class, then just include a test in the constructor and have it throw an error or assert. Don't worry about making it *impossible* to create multiple instances... it's just not worth the effort. If people are determined to use your classes incorrectly and disregard warnings and errors, etc, then they're digging their own grave. Here's the implementation I use: public class Singleton { private static _instance:Singleton; public function get instance():Singleton { if (_instance == null) _instance = new Singleton(); return _instance; } public function Singleton() { if (_instance != null) throw new Error("This class is a singleton. Instance already created."); } } Troy.
[flexcoders] Re: Anyone have a good resource on singletons?
Hi, here I got a way to do it here http://code.google.com/p/maashaack/wiki/Singleton note that a lot of people will debate on how to define singleton in AS3, I don't say that the solution I mention above is the best but imho it's the most standard-way to do it and the closest to the AS3 language (and if someone want to debate that come with good arguments, just saying "duh Java use getInstance() so we should do the same" that will not qualify as a good argument) cheers, zwetan --- In flexcoders@yahoogroups.com, "Scott" <[EMAIL PROTECTED]> wrote: > > I'm trying to find information on using singletons in AS3/Flex. I've > got an .AS file set up but I'm having issues calling the data/functions > within that function in other classes. Does anyone have a good resource > on the web for creating and using singletons? > > > > Thanks! >
Re: [flexcoders] Re: Anyone have a good resource on singletons?
Well that makes it less dangerous and it's good to know, cheers. No less ugly though, I just don't like it. I'd prefer a public interface, and instantiation hidden behind a locator or injection of some sort. IMO anything (in your API) that's static and not a "proper" Function (ie no side-effects) should be seriously thought over. -Josh On Thu, Jul 17, 2008 at 9:11 AM, Maciek Sakrejda <[EMAIL PROTECTED]> wrote: > For what it's worth, that doesn't pollute the global namespace, Josh. > Things within a file outside of the package declared in that file are > private to the class (hence, others can't instantiate your Singleton > unless they pass it null). Kind of odd behavior in ActionScript, but > meh... > > But you're right: opinions on Singletons in general are mixed at best. > Check out, say, > http://steve.yegge.googlepages.com/singleton-considered-stupid > > > > -- "Therefore, send not to know For whom the bell tolls. It tolls for thee." :: Josh 'G-Funk' McDonald :: 0437 221 380 :: [EMAIL PROTECTED]
Re: [flexcoders] Re: Anyone have a good resource on singletons?
For what it's worth, that doesn't pollute the global namespace, Josh. Things within a file outside of the package declared in that file are private to the class (hence, others can't instantiate your Singleton unless they pass it null). Kind of odd behavior in ActionScript, but meh... But you're right: opinions on Singletons in general are mixed at best. Check out, say, http://steve.yegge.googlepages.com/singleton-considered-stupid -- Maciek Sakrejda Truviso, Inc. http://www.truviso.com -Original Message- From: Josh McDonald <[EMAIL PROTECTED]> Reply-To: flexcoders@yahoogroups.com To: flexcoders@yahoogroups.com Subject: Re: [flexcoders] Re: Anyone have a good resource on singletons? Date: Thu, 17 Jul 2008 08:44:50 +1000 Make that *public* static function init (root : DisplayObject) http://nondocs.blogspot.com/2007/04/metadatamixin.html -Josh On Thu, Jul 17, 2008 at 8:44 AM, Josh McDonald <[EMAIL PROTECTED]> wrote: **Rant (and code) follows** Ew, I hate that pattern. Well I'm not fond of client-aware singleton-dom either, but ew. What the hell does that "outside the package" definition do? Add a package-protected class to the default package? Add stuff to Global? Disappear it into fat air? What about name conflicts? If you're stuck with non-injection, try to use an interface and put "getInstance" into a public static helper / locator function in a public "Locator" class, that instantiates a package-protected class with public methods. Since we can't hide the constructor, all "enforce singleton" patterns are going to be ugly in AS3. I know two files are a pain, but try and keep the voodoo inside your package and out of the global namespace :) If you want "new SingletonClass()" to fail at compile-time like the above example, create a proper class with a custom namespace to do the enforcing instead of the throwaway default package class. Call it "DONTUSEME::ThisIsASingleton" if you wanna make things clear :) You can also enforce it at runtime if you like, using something like the below code. Oh, and make it "function get instance()" instead of "function getInstance()", this is ActionScript. If you're stuck doing a one-file singleton because you're too used to Java *ducks*, do it something like this: [Mixin] public final class SingletonClass { static private var __enforceSingletonKey : Number; static private var __instance : SingletonClass; static function init (root : DisplayObject) : void { //It's just safer to do it here than on the static declaration above. And you'll dodge some compiler bugs that misreport errors :) __enforceSingletonKey = Math.random(); } function SingletonClass(key : Number) { if (key != __enforceSingletonKey) throw new Error("Don't instantiate me bro!"); } public static function get instance() : SingletonClass { if (!__instance) __instance = new SingletonClass(__enforceSingletonKey); return instance; } } -Josh On Thu, Jul 17, 2008 at 4:55 AM, dbronk <[EMAIL PROTECTED]> wrote: Here is my preferred way of creating a singleton class. Usage would be: MySingleton.instance.someVar Dale package your.package { [Bindable] final public class MySingleton { public static const instance : MySingleton= new MySingleton(SingletonLock); public var someVar : String = "Hello"; public function MySingleton(lockClass:Class) { if ( lockClass != SingletonLock || instance != null ) { throw new Error("Invalid instantiation. Please use instance."); } } } } class SingletonLock {}
Re: [flexcoders] Re: Anyone have a good resource on singletons?
Make that *public* static function init (root : DisplayObject) http://nondocs.blogspot.com/2007/04/metadatamixin.html -Josh On Thu, Jul 17, 2008 at 8:44 AM, Josh McDonald <[EMAIL PROTECTED]> wrote: > **Rant (and code) follows** > > Ew, I hate that pattern. Well I'm not fond of client-aware singleton-dom > either, but ew. What the hell does that "outside the package" definition do? > Add a package-protected class to the default package? Add stuff to Global? > Disappear it into fat air? What about name conflicts? > > If you're stuck with non-injection, try to use an interface and put > "getInstance" into a public static helper / locator function in a public > "Locator" class, that instantiates a package-protected class with public > methods. > > Since we can't hide the constructor, all "enforce singleton" patterns are > going to be ugly in AS3. I know two files are a pain, but try and keep the > voodoo inside your package and out of the global namespace :) > > If you want "new SingletonClass()" to fail at compile-time like the above > example, create a proper class with a custom namespace to do the enforcing > instead of the throwaway default package class. Call it > "DONTUSEME::ThisIsASingleton" if you wanna make things clear :) You can also > enforce it at runtime if you like, using something like the below code. > > Oh, and make it "function get instance()" instead of "function > getInstance()", this is ActionScript. > > If you're stuck doing a one-file singleton because you're too used to Java > *ducks*, do it something like this: > > [Mixin] > public final class SingletonClass { > > static private var __enforceSingletonKey : Number; > static private var __instance : SingletonClass; > > static function init (root : DisplayObject) : void > { > //It's just safer to do it here than on the static declaration above. > And you'll dodge some compiler bugs that misreport errors :) > __enforceSingletonKey = Math.random(); > } > > function SingletonClass(key : Number) > { > if (key != __enforceSingletonKey) throw new Error("Don't instantiate me > bro!"); > } > > public static function get instance() : SingletonClass > { > if (!__instance) > __instance = new SingletonClass(__enforceSingletonKey); > > return instance; > } > } > > -Josh > > > On Thu, Jul 17, 2008 at 4:55 AM, dbronk <[EMAIL PROTECTED]> wrote: > >> Here is my preferred way of creating a singleton class. Usage would >> be: MySingleton.instance.someVar >> >> >> Dale >> >> >> >> package your.package >> { >> >> [Bindable] >> final public class MySingleton >> { >> public static const instance : MySingleton= new >> MySingleton(SingletonLock); >> >> public var someVar : String = "Hello"; >> >> public function MySingleton(lockClass:Class) >> { >> if ( lockClass != SingletonLock || instance != null ) >> { >> throw new Error("Invalid instantiation. Please use instance."); >> } >> } >> >> } >> } >> >> class SingletonLock {} >> >> >> >> >> >> >> >> --- In flexcoders@yahoogroups.com, "Tracy Spratt" <[EMAIL PROTECTED]> wrote: >> > >> > A singleton class will have a static getInstance() method. >> > >> > >> > >> > Anywhere you need to access members of the singleton, call that method. >> > Just import the class, do not use the normal instantiation procedures, >> > like new, or using an mxml tag. >> > >> > import mypackage.MyModel; >> > >> > [Bindable]private var _myModel:MyModel; //optional var reference >> > >> > >> > >> > Private function init(): void >> > >> > _myModel = MyModel.getInstance(); >> > >> > Alert.show(_myModel.myFunction()) >> > >> > >> > >> > > > >> > >> > >> > Tracy >> > >> > >> > >> > >> > >> > From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On >> > Behalf Of Scott >> > Sent: Wednesday, July 16, 2008 12:42 PM >> > To: flexcoders@yahoogroups.com >> > Subject: [flexcoders] Anyone have a good resource on singletons? >> > >> > >> > >> > I'm trying to find information on using singletons in AS3/Flex. I've >> > got an .AS file set up but I'm having issues calling the data/functions >> > within that function in other classes. Does anyone have a good resource >> > on the web for creating and using singletons? >> > >> > >> > >> > Thanks! >> > >> >> >> >> >> >> -- >> Flexcoders Mailing List >> FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt >> Search Archives: >> http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo! Groups >> Links >> >> >> >> > > > -- > "Therefore, send not to know For whom the bell tolls. It tolls for thee." > > :: Josh 'G-Funk' McDonald > :: 0437 221 380 :: [EMAIL PROTECTED] > -- "Therefore, send not to know For whom the bell tolls. It tolls for thee." :: Josh 'G-Funk' McDonald :: 0437 221 380 :: [EMAIL PROTECTED]
Re: [flexcoders] Re: Anyone have a good resource on singletons?
**Rant (and code) follows** Ew, I hate that pattern. Well I'm not fond of client-aware singleton-dom either, but ew. What the hell does that "outside the package" definition do? Add a package-protected class to the default package? Add stuff to Global? Disappear it into fat air? What about name conflicts? If you're stuck with non-injection, try to use an interface and put "getInstance" into a public static helper / locator function in a public "Locator" class, that instantiates a package-protected class with public methods. Since we can't hide the constructor, all "enforce singleton" patterns are going to be ugly in AS3. I know two files are a pain, but try and keep the voodoo inside your package and out of the global namespace :) If you want "new SingletonClass()" to fail at compile-time like the above example, create a proper class with a custom namespace to do the enforcing instead of the throwaway default package class. Call it "DONTUSEME::ThisIsASingleton" if you wanna make things clear :) You can also enforce it at runtime if you like, using something like the below code. Oh, and make it "function get instance()" instead of "function getInstance()", this is ActionScript. If you're stuck doing a one-file singleton because you're too used to Java *ducks*, do it something like this: [Mixin] public final class SingletonClass { static private var __enforceSingletonKey : Number; static private var __instance : SingletonClass; static function init (root : DisplayObject) : void { //It's just safer to do it here than on the static declaration above. And you'll dodge some compiler bugs that misreport errors :) __enforceSingletonKey = Math.random(); } function SingletonClass(key : Number) { if (key != __enforceSingletonKey) throw new Error("Don't instantiate me bro!"); } public static function get instance() : SingletonClass { if (!__instance) __instance = new SingletonClass(__enforceSingletonKey); return instance; } } -Josh On Thu, Jul 17, 2008 at 4:55 AM, dbronk <[EMAIL PROTECTED]> wrote: > Here is my preferred way of creating a singleton class. Usage would > be: MySingleton.instance.someVar > > > Dale > > > > package your.package > { > > [Bindable] > final public class MySingleton > { > public static const instance : MySingleton= new > MySingleton(SingletonLock); > > public var someVar : String = "Hello"; > > public function MySingleton(lockClass:Class) > { > if ( lockClass != SingletonLock || instance != null ) > { > throw new Error("Invalid instantiation. Please use instance."); > } > } > > } > } > > class SingletonLock {} > > > > > > > > --- In flexcoders@yahoogroups.com, "Tracy Spratt" <[EMAIL PROTECTED]> wrote: > > > > A singleton class will have a static getInstance() method. > > > > > > > > Anywhere you need to access members of the singleton, call that method. > > Just import the class, do not use the normal instantiation procedures, > > like new, or using an mxml tag. > > > > import mypackage.MyModel; > > > > [Bindable]private var _myModel:MyModel; //optional var reference > > > > > > > > Private function init(): void > > > > _myModel = MyModel.getInstance(); > > > > Alert.show(_myModel.myFunction()) > > > > > > > > > > > > > > > Tracy > > > > > > > > > > > > From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On > > Behalf Of Scott > > Sent: Wednesday, July 16, 2008 12:42 PM > > To: flexcoders@yahoogroups.com > > Subject: [flexcoders] Anyone have a good resource on singletons? > > > > > > > > I'm trying to find information on using singletons in AS3/Flex. I've > > got an .AS file set up but I'm having issues calling the data/functions > > within that function in other classes. Does anyone have a good resource > > on the web for creating and using singletons? > > > > > > > > Thanks! > > > > > > > > -- > Flexcoders Mailing List > FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt > Search Archives: > http://www.mail-archive.com/flexcoders%40yahoogroups.comYahoo! Groups > Links > > > > -- "Therefore, send not to know For whom the bell tolls. It tolls for thee." :: Josh 'G-Funk' McDonald :: 0437 221 380 :: [EMAIL PROTECTED]
[flexcoders] Re: Anyone have a good resource on singletons?
Here is my preferred way of creating a singleton class. Usage would be: MySingleton.instance.someVar Dale package your.package { [Bindable] final public class MySingleton { public static const instance : MySingleton= new MySingleton(SingletonLock); public var someVar : String = "Hello"; public function MySingleton(lockClass:Class) { if ( lockClass != SingletonLock || instance != null ) { throw new Error("Invalid instantiation. Please use instance."); } } } } class SingletonLock {} --- In flexcoders@yahoogroups.com, "Tracy Spratt" <[EMAIL PROTECTED]> wrote: > > A singleton class will have a static getInstance() method. > > > > Anywhere you need to access members of the singleton, call that method. > Just import the class, do not use the normal instantiation procedures, > like new, or using an mxml tag. > > import mypackage.MyModel; > > [Bindable]private var _myModel:MyModel; //optional var reference > > > > Private function init(): void > > _myModel = MyModel.getInstance(); > > Alert.show(_myModel.myFunction()) > > > > > > > Tracy > > > > > > From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On > Behalf Of Scott > Sent: Wednesday, July 16, 2008 12:42 PM > To: flexcoders@yahoogroups.com > Subject: [flexcoders] Anyone have a good resource on singletons? > > > > I'm trying to find information on using singletons in AS3/Flex. I've > got an .AS file set up but I'm having issues calling the data/functions > within that function in other classes. Does anyone have a good resource > on the web for creating and using singletons? > > > > Thanks! >