Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 12-22-2008, 05:18 AM   #1
rawmantick
Pipty rupees only!
 
rawmantick's Avatar
 
Join Date: Jul 2007
Location: Germany
Posts: 1,730
Default ActionScript 3 Singleton

I've been meeting several solutions of implementing singleton classes in AS3. SInce we cannot set the constructor as private, many "hacks" have the right to appeart. However, the most commonly used variant (which is described in ActionScript 3.0 Design Patterns from O'Reilly) is very weird to my opinion. The constructor requires some class, which can only be the private one, being hidden from another parts of the program.
The common situation is: the developer meets the class. He(she) wants to instantiate it and gets runtime error like "use getInstance instead". And he(she) does. But what the hack, I need to pass some sort strange and unknown class... It will realy make the developer jump into 5 minutes of thinking "what???".

Probably I won't invent anything. But a very easy and ellegant way if "singletoning" is offered:
ActionScript Code:
public function SingletonClass {     static public function getInstance():SingletonClass     {         if( _instance == null )         {             _allowCallConstructor = true;             _instance = new SingletonClass();             _allowCallConstructor = false;         }         return _instance;     }     static private var _instance:SingletonClass = null;     static private var _allowCallConstructor:Boolean = false;     public function SingletonClass()     {         if( !_allowClassConstructor )             throw new Error("Use getInstance() method instead");         ...     } }
__________________
Bang!

Last edited by rawmantick; 12-22-2008 at 05:21 AM.
rawmantick is offline   Reply With Quote
Old 12-22-2008, 07:37 AM   #2
creynders
flash veteran
 
creynders's Avatar
 
Join Date: May 2005
Location: Belgium
Posts: 914
Default

All singleton implementations have several drawbacks and other benefits.
This one has as a drawback that it doesn't throw any compile-time errors if you DO try to instantiate. Hence the use of the internal class that you've seen in the other implementations.
However, you're incorrect in thinking that you need to use it in conjunction with getInstance. It provides a compile-time checking mechanism and as the "user" of the singleton class you don't need to do anything with the internal class.
Here's the basic implementation:
ActionScript Code:
package {     public class SingletonClass {         static private var instance : SingletonClass;                 static public function getInstance() : SingletonClass {             if ( instance == null ) instance = new SingletonClass( new Lock() );             return instance;         }                 public function( lock : Lock ) : void {             if ( lock == null ) throw new Error( "not allowed bla bla bla ..." );         }             } } internal class Lock{}
You'll use it like this:
ActionScript Code:
SingletonClass.getInstance().someMethod();
However if you'd try to do this
ActionScript Code:
s : SingletonClass = new SingletonClass()
You'd get a Compile Time Error saying a required parameter has not been provided.
I think that's an added benefit, but of course that's a matter of taste.
creynders is offline   Reply With Quote
Old 12-22-2008, 08:19 AM   #3
rawmantick
Pipty rupees only!
 
rawmantick's Avatar
 
Join Date: Jul 2007
Location: Germany
Posts: 1,730
Default

Anyway since having got a private constructor we would start to feel free of these hacks...

// not only me putting static before access specifier )))
__________________
Bang!
rawmantick is offline   Reply With Quote
Old 12-22-2008, 12:42 PM   #4
newblack
dondeEstanMisPantalones?
 
newblack's Avatar
 
Join Date: Nov 2005
Location: New York Proper
Posts: 1,355
Send a message via AIM to newblack
Default

i've been too lazy for a long time to do anything in the constructor- it doesn't end up being worth it in the end. how many times in the development of your application has that error gotten thrown apart from testing your singleton implementation itself?

that said, i have to say i don't like romantique's implementation- you're choosing to store a boolean that only ever equates to whether or not _instance is defined or null. and especially since you do that check anyhow, the boolean is superfluous.
__________________
i am gibreel farishta
general relativity
jellytanks alpha redux
newblack is offline   Reply With Quote
Old 12-22-2008, 05:46 PM   #5
rawmantick
Pipty rupees only!
 
rawmantick's Avatar
 
Join Date: Jul 2007
Location: Germany
Posts: 1,730
Default

The variable is not superflous. And you are not right about the boolean. It's for another thing. It is a key whether it's allowed to use constructor or not. And not for checking if the instance is already created or not. Anyway... after minutes of rethinking I agree with creynders - my variant goes for runtime, which is not commonly used. The more common situation is checking "singleton" at compile.

Eh... anyway, I don't use singletons too often) And to say generaly - who realy cares which way to go if it works
__________________
Bang!

Last edited by rawmantick; 12-22-2008 at 05:51 PM.
rawmantick is offline   Reply With Quote
Old 12-22-2008, 06:02 PM   #6
newblack
dondeEstanMisPantalones?
 
newblack's Avatar
 
Join Date: Nov 2005
Location: New York Proper
Posts: 1,355
Send a message via AIM to newblack
Default

it is superfluous. at least how you're using it here. the condition only represents whether the instance is null or not. it is the exact same thing as throwing an error in the constructor if _instance == null.
__________________
i am gibreel farishta
general relativity
jellytanks alpha redux
newblack is offline   Reply With Quote
Old 12-22-2008, 07:02 PM   #7
rawmantick
Pipty rupees only!
 
rawmantick's Avatar
 
Join Date: Jul 2007
Location: Germany
Posts: 1,730
Default

With this boolean you cannot call a constructor from anywhere, only from where you can change that boolean - from getInstance() method.

In getInstance() I check if the instance already exists. If so - I return it. If not - I allow instantiating only for one and the only one time, instantiate and finaly forbid it forever.

Checking for null is for single instance. Checking for boolean value is for accessing constructor.

You can draw a state diagram with all the possible state sets and you will see, that there are situations, when instance==null and allowCallConstructor==true conditions are not always together of the same value. Which absolutely prove tha fact, that the boolean is not superflous. It just has another thing to control.

I am sure.

// update
Quote:
it is the exact same thing as throwing an error in the constructor if _instance == null
Ok. The instance is null. And you throw an error if an instance is null from constructor. You use getInstance() instead. Inside getInstance() you try to instantiate. But when you try to instantiate - you call a constructor. Inside this call instance is still null. The constructor throws an error anyway, no matter if you call it from somewhere of from getInstance. That's what you are describing. And that's exactly what the boolean is for to resolve the cycle. I think this is obvious. Don't you think? I don't see if it's possible for an example you've described to work. I've tested my one. Yes it does work. Well... ofcourse if I haven't missed anything from your words

Sory for my english...
__________________
Bang!

Last edited by rawmantick; 12-22-2008 at 07:13 PM.
rawmantick is offline   Reply With Quote
Old 12-22-2008, 07:14 PM   #8
newblack
dondeEstanMisPantalones?
 
newblack's Avatar
 
Join Date: Nov 2005
Location: New York Proper
Posts: 1,355
Send a message via AIM to newblack
Default

haha, i'm sorry you had to write all that, i've been eating knuckle sandwiches again, of course what i've suggested wouldn't work.

all of that said, i do think it seems like too much effort still... but sorry for the hassle!
__________________
i am gibreel farishta
general relativity
jellytanks alpha redux
newblack is offline   Reply With Quote
Old 12-22-2008, 07:17 PM   #9
rawmantick
Pipty rupees only!
 
rawmantick's Avatar
 
Join Date: Jul 2007
Location: Germany
Posts: 1,730
Default

Anyway thank you guys newblack and creynders for discussion and opinions
__________________
Bang!
rawmantick is offline   Reply With Quote
Old 12-24-2008, 09:16 AM   #10
creynders
flash veteran
 
creynders's Avatar
 
Join Date: May 2005
Location: Belgium
Posts: 914
Default

Quote:
Originally Posted by romantique View Post
Anyway since having got a private constructor we would start to feel free of these hacks...

// not only me putting static before access specifier )))
Ah yes, don't you think that's a lot clearer and more readable?
IMO something which is noteworthy (like a property being static, which is a huge difference) should be most visible and draw attention to itself.
creynders is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Extended Singleton issues daveystew ActionScript 2.0 1 03-01-2008 09:48 PM
I'm beginner in actionscript. I have questions. helgso ActionScript 1.0 (and below) 7 06-16-2006 04:11 PM
Flash Actionscript Career Advice? bdemil General Chat 3 06-15-2006 06:35 PM
Need Help! displaying external text files Flash_Boi ActionScript 2.0 7 03-30-2006 08:23 PM
ActionScript Confusion stanonbass ActionScript 1.0 (and below) 7 12-07-2004 08:16 PM


All times are GMT. The time now is 03:29 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.