| Home | Tutorials | Forums | Articles | Blogs | Movies | Library | Employment | Press | Buy templates |
|
|
#1 |
|
Pipty rupees only!
Join Date: Jul 2007
Location: Novosibirsk, Russia
Posts: 1,713
|
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:
__________________
fly high Last edited by rawmantick; 12-22-2008 at 06:21 AM.. |
|
|
|
|
|
#2 |
|
flash veteran
Join Date: May 2005
Location: Belgium
Posts: 899
|
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:
ActionScript Code:
ActionScript Code:
I think that's an added benefit, but of course that's a matter of taste. |
|
|
|
|
|
|
|
|
#4 |
|
dondeEstanMisPantalones?
|
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. |
|
|
|
|
|
#5 |
|
Pipty rupees only!
Join Date: Jul 2007
Location: Novosibirsk, Russia
Posts: 1,713
|
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 ![]()
__________________
fly high Last edited by rawmantick; 12-22-2008 at 06:51 PM.. |
|
|
|
|
|
#6 |
|
dondeEstanMisPantalones?
|
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.
|
|
|
|
|
|
#7 | |
|
Pipty rupees only!
Join Date: Jul 2007
Location: Novosibirsk, Russia
Posts: 1,713
|
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:
![]() Sory for my english...
__________________
fly high Last edited by rawmantick; 12-22-2008 at 08:13 PM.. |
|
|
|
|
|
|
#8 |
|
dondeEstanMisPantalones?
|
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! |
|
|
|
|
|
#10 | |
|
flash veteran
Join Date: May 2005
Location: Belgium
Posts: 899
|
Quote:
IMO something which is noteworthy (like a property being static, which is a huge difference) should be most visible and draw attention to itself. |
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Extended Singleton issues | daveystew | ActionScript 2.0 | 1 | 03-01-2008 10:48 PM |
| I'm beginner in actionscript. I have questions. | helgso | ActionScript 1.0 (and below) | 7 | 06-16-2006 05:11 PM |
| Flash Actionscript Career Advice? | bdemil | General Chat | 3 | 06-15-2006 07:35 PM |
| Need Help! displaying external text files | Flash_Boi | ActionScript 2.0 | 7 | 03-30-2006 09:23 PM |
| ActionScript Confusion | stanonbass | ActionScript 1.0 (and below) | 7 | 12-07-2004 09:16 PM |