We've had other singleton discussions before so I won't re-hash, but
will point out that we don't have the same thread/locking problem that
Java does when accessing the singleton because your AS code is
single-threaded.  So doing the if (instance == null) instance = new...
is just fine.

Matt

-----Original Message-----
From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of crnet_flex
Sent: Friday, April 14, 2006 3:19 PM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Re: Singleton not usable?

--- In flexcoders@yahoogroups.com, "Sonja Duijvesteijn" 
<[EMAIL PROTECTED]> wrote:
>
> Hi all,
> 
> I'm trying to make a singleton class as demonstrated below, but 
get the
> error: "a constructor may only be declared 'public'". Well, that 
error is
> clear, the reason for it is not however.
> Isn't it possible anymore to make a singleton class, or is there 
another
> method for it?
> 
> Class Singleton {
> 
> private static var _instance:Singleton = null;
> 
> private function Sinleton() {
> }
> 
> public static function getInstance():Singleton {
>    if(Singleton._instance == null) {
>       _instance = new Singleton();
>    }
>    return _instance;
> }
> 
> }
> 
> Any help would be much appreciated.
> 
> Kind regards,
> Sonja Duijvesteijn
>

Hi

Have anyone tried using a construction like the following?

public class Singleton
{
        public static const Instance:Singleton = new Singleton();

        function Singleton()
        {
        } 
                        
        public function doSomething():void
        {
        }
}

This is actually the recommandations of creating a singleton in c#, 
and it seems that it fullfils the job here as well.

I know that, if you use flexbuilder 2 beta, then you'll get a 
warning if you leave out the scope of the constructor, but maybe 
it's worth it.

By the way, be aware that the singleton posted initially as an 
example is not a recommended way of creating a singleton, i guess 
it's not a problem in AS3, but lazy loaded singletons must have a 
syncronized/looked load section when thread safety is an issue.

BR Casper





--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives:
http://www.mail-archive.com/flexcoders%40yahoogroups.com 
Yahoo! Groups Links



 




--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/flexcoders/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to