That is true, my design is based on the getInstance(), and I got a
private constructor.

The new FontLoader() below is a sample that I meant just was needed to
solve my listeners-problems ( when getInstance() is used in several
classes with listeners setup)

I am really questioning everything in my code, in which really isn't
necessary, but I cant help it for now.

The listeners-problem is solved if I always removes the listeners when
the loading of fonts has been done, so that I can use the
FontLoader-instance in another class without firing the other
event-listeners in other classes.

/ martin 

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of James
O'Reilly
Sent: den 28 oktober 2005 08:05
To: Flashcoders mailing list
Subject: Re: [Flashcoders] Singleton and Listeners is a bad combination

Maybe I'm misunderstanding the implementation of the Singleton but I 
thought a statement like this was impossible?  Doesn't the singleton 
have a private constructor that only a public method such as 
getInstance() can access?  I didn't think you could use "new" outside of

the class itself like you did below:

   Class A
   private var fntController:FontLoader = new FontLoader();

   Class B
   private var fntController:FontLoader = new FontLoader();


However, I think you're on the right track with making FontLoader a 
singlton.  It sounds like something you probably don't need more than 
one of tho I don't really know what it does for sure.

One way to get this to work, also mentioned by Alan, is having the 
FontLoader pass an event object back which could be examined by each of 
the listening classes.

The other is for FontLoader to have multiple listener lists and have 
each class subscribe to a different list.  Something like 
addPixelFontListener() and addNormalFontListener() for example would 
enable all of your class to subscribe to either one or both.  Keep in 
mind this could get out of hand quickly if you keep adding more and more

list types.

There are probably other solutions as well.

JOR


___________________________________
===  James O'Reilly
===
===  SynergyMedia, Inc.
===  www.synergymedia.net



Martin Klasson wrote:
>  
> 
> Hi Coders.
> 
>  
> 
> I got a class which I thought would be improved by using the Singleton
pattern. And it does, until I am adding in the EventDispatcher.
> 
> The problem is that my application will using the
FontLoader.getInstance() at several places in different classes and
files.
> 
> The problem arises since you can set addEventListeners to the
instance.
> 
>  
> 
> If you in class A has:
> 
> fntController = FontLoader.getInstance()
> 
> fntController.addEventListener('onFinished', fontsLoaded)
> 
>  
> 
> And in Class B you have the same as class A, just to state an example
of the problem...
> 
>  
> 
> The problem is that since FontLoader.getInstance() returns exactly the
same instance everytime. So when class B fontsLoaded is called, the
fontsLoaded is also called in class A.
> 
> The problem is that even though you can use removeEventListener, you
might not want do that since you might still want the fontsLoaded to be
working in class A another time.
> 
> One solution is that you in class A and class B doesnt have the same
name for the function that is given in the addEventListener -but that
doesnt seem like a neat solution to depend on that when several
developers might write the same.
> 
> As well as boring for the EventDispatcher used in the FontLoader to
call listener-function which might not exist in the different
scopes/classes where the FontLoader istance is.
> 
>  
> 
> So I thought the best way would be to do the class ALL static, but the
problem would still be the same with the listeners.
> 
> I can only come up with one solution, and that is to do the usual,
that you will have to instanciate the class.
> 
>  
> 
> class A
> 
> private var fntController:FontLoader = new FontLoader(); // now I can
have listeners which just would listen to this instance.
> 
> class B
> 
> private var fntController:FontLoader = new FontLoader(); // now I can
have listeners which just would listen to this instance.
> 
>  
> 
> This will make them able to have listeners that wont interfere with
each other. Is this the best solution, or do you know of any other
solution that you think would suit this scenario?
> 
> Listeners are incredibly great, and I am thinking on the Key-class,
that has addListener, which is a static class and should suffer just as
well as my FontLoader-listeners does.  But somehow it doesnt fit the
FontLoader.
> 
> What do you think, any general advices?
> 
>  
> 
> Singleton and EventDispatcher doesnt come in handy when you are having
the getInstance() at several places in an application, or am I wrong?
> 
> Thanks.
> 
> / martin
> 
> 
> 
>
------------------------------------------------------------------------
> 
> _______________________________________________
> Flashcoders mailing list
> [email protected]
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders



_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to