Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do singletons pervade ActionScript culture?

I have recently started doing some ActionScript/Flex programming and I am... Surprised... By the number of singletons I see. They are everywhere! The standard library, the frameworks... Heck, I even read a blog post this morning where the author mentioned that he refactored some of his classes INTO singletons!

Is there any reasonable explanation for why the AS community likes singletons so much?

like image 789
David Wolever Avatar asked Jun 15 '09 20:06

David Wolever


3 Answers

It's not just the AS community, it's a general trend in software design. Component-oriented programming encourages the initial setup and wiring of service objects, which then collaborate using transient messages. Singletons are a natural part of this idea.

Also singletons reduce the runtime impact of objects that are expensive to create.

like image 181
skaffman Avatar answered Oct 16 '22 05:10

skaffman


I think it comes from the old way Flash used to work... In those old days there were not many heavy programmers doing flash, just a few, and they were considered prodigies (and rightfully so). Most of the people were people who transferred from the print-industry... they thought that Flash was the new Illustrator for the webz.

In the old days flash developers used the easy-to-use 'TellTarget' to get to a certain MovieClip which could be nested inside a MovieClip inside a MovieClip inside... etc. It simply was the way it was done... those people (including me) never had any software background, so we lived in this visual world and Flash was thinking the way designers thought. In AS2 lots of people (those who were and those who weren't good at coding) also had lots of problems losing 'scopes' inside classes... I remember the Proxy-class which helped us not to loose the scope in a class. Lots of headaches these days. The people who gradually upgraded their wisdom of code never wrapped their heads entirely around the new ways of coding in OOP... some of them think a Singleton can be used as a kind of 'global'. They can always talk to this 'global' from anywhere in their application.

I think it's that simple...

like image 5
Ypmits Avatar answered Oct 16 '22 04:10

Ypmits


I'm not really sure where it's all coming from, Flex is full of them, and as you mention, we see them in lot's of other places. They do look attractive at first, but they almost always lead to a headache somewhere down the road.

Everyone should read Miško Hevery' blog (start with this one) He makes some excellent arguments against using them.

Singleton's do have there use's, a Logger for example, but they are way over used.

@skaffman In regards to the performance benefits, it's perfectly reasonable to have single instance of an object in your application without implementing a Singleton Pattern.

like image 3
Tyler Egeto Avatar answered Oct 16 '22 03:10

Tyler Egeto