views:

457

answers:

5

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?

+6  A: 

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.

skaffman
I find this ironic because, to me, extensively using singletons feels like using a functional programming language.
Kai
Well there are no cut-and-dried boundaries between these techniques. Good practice evolves to borrow the best bits from them all.
skaffman
@Kai: Really? Why's that? To me it feels like exactly the opposite: singletons promote global state (so discourage idempotence), whereas functional languages encourage pure functions (which strongly discourage global state/encourage idempotence).
David Wolever
Out of curiosity, could you cite any sources showing that it's an increasing trend in software design? I don't have enough experience to say either way.
David Wolever
No, but I've been doing this for 13 years, and I'm telling you, it's a trend :)
skaffman
Unfortunately for the software industry, I don't doubt you :(
David Wolever
It's a trend? I thought the tide had turned against singletons.
Nosredna
Component-oriented programming is exaclty the aproach I've taken based on my observation of the space.
Fire Crow
The tide has turrned against the singleton *design pattern*, not singletons in general.
skaffman
+3  A: 

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.

Tyler Egeto
Singletons don't require the use of the Singleton design pattern. In fact, the pattern (as specified in the GoF book) is is rather unpleasant and crude. Better to enforce singletons through the application framework.
skaffman
Fantastic article, thanks for the link!
Soviut
+5  A: 

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...

Ypmits
A: 

I've since changed my position in this regard.

Using objects in actionscript3 haas served me well to increase modulatiry.

I'd speculate that like me, the benefits of OOP are consusing and hard to come by for most begining flash developers.

I think there are really two reasons for this.

1.) talking about the basics is boring, so most information is so overcomplicated. OOP is really just breaking a system into understandable reusable parts.

2.) procedural programming is similar to the workflow, it locks you into a certain work flow, but for begining programmers that makes it easier to comprehend

3.) Alot of flash is about managing the state of the flash widget, so it makes some level of sense to manage the information in one central place.

---------------- original response ----------------

I'm part of that AS community that likes (and uses) singletons.

I'm relatively new to programming (3 years PHP/MySQL/Javascript/Actionscript proffesionally).

I think there is a clear distinction between modular programming and classical class based inheritance OOP.

Modular programming, which from what I understand is large part of OOP, is an extremely important part of effective program design.

Anything used more than once can be moved into a seperate function. Groups of operations that share similar functionality or attributes can be seperated into their common parts and then the different parts uniqe to thier instance.

However, I find it more effective to do this with a collection of interrelated modules

In my opinion most of inheritance base OOP is just hot air. Modules interacting with each other is a much more efficient way to model the activities of a program that 15 versions of basically the same thing.

I make a core and make modules to atatch to the core, but never have the sub-object be an extension of the core. I think extending a class is cumbersome and stagnates development by removing necessary flexibility from it.

One of the reasons I've often read (and heard behind inheritence based OOP) is that it increases readability and maintainablity. To me all this means is that programmers would rather use a system that's easy for them, than a system effective for the program.

I am all for writing as clean and understandable code as possible but only up to and not past the point where it inhibits the creativity and flexibility of a program.

I'm for OOP but I prefer a singleton, prototype, or module based aproach.

Fire Crow
You neglect the fact that the inheritance model also enforces type safety. This is a big deal if you're making a game where all game objects derive from a single object, all destructible objects inherit the destructible interface, etc. Inheritance isn't just about overrides.
Soviut
Tt still sounds like shooting a fly with a cannon ball to me, the objects should be more organized than in one big bucket. Asign the destructors for each group, or make a seperate destructor module as one global object and have all the objects talk to that. The game you describe sounds like no fun, because the best games are the ones with variety.
Fire Crow
Out of curiosity, do you perform unit, or any other form of automated testing on your code?
David Wolever
Yes I thoroughly test each unit of my programs individually as they are developed. usually I have a 'spoofer' version of the main script that runs each module through a workflow for me to ensure it is functioning property. I do however do this process manually it is not automated.
Fire Crow
Ah, I see – thanks. One of the the strongest arguments I've seen (and experienced) against using singletons (aka global variables) is that they make unit testing hard, so I was curious how you did it.
David Wolever
+3  A: 

To the excellent points that have already been made here, I would add that there is something about the high speed in which Flash projects are put together and the fact that most will not need to be maintained, that encourages people to just get things done as quickly as possible, and Singletons are a good way to make that happen, and you can use getInstance rather than passing around references, dependancy injection, etc. In practice, the fact is that you are only ever going to have one instance of many of your classes, even though in theory you might want more than one.

Iain