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
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to