Hiya,
I reckon that it would be a good idea not to make it a singleton but
create new instances every time. The fact that you are running into the
problem with listeners is a pretty clear indication that you require new
instances to be created for every use of the class - not really the
right scenario for a singleton.
The FontLoader class could hold a static list of fonts that are already
loaded for internal reference - but the SWFs that are loading the fonts
don't need to know about that kind of logic - they simply need to know
that they can use the FontLoader class to load a font that they require.
Hope that helps,
Nils.
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
--

-----------------------------------------------------------------------
Nils Millahn
Internet Solutions - Design and Development - Flash Specialist
T: +44 (0) 7909 528 617
E: [EMAIL PROTECTED]
W: www.hub124.co.uk
=======================================================================
Notice of Confidentiality.
This transmission is intended for the named recipient only. It contains
information which may be confidential and which may also be privileged. Unless
you are the named addressee (or authorised to receive it for the addressee) you
may not copy or use it, or disclose it to anyone else.
It is the responsibility of the recipient to ensure that the forwarding,
opening or use of the e-mail (and any attachment) will not adversely
affect their system or data. Please carry out appropriate virus checks.
=======================================================================
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders