Reputation: 154494
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?
Upvotes: 6
Views: 579
Reputation: 7729
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.
Upvotes: -1
Reputation: 9442
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.
Upvotes: 3
Reputation: 388
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...
Upvotes: 5
Reputation: 5495
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.
Upvotes: 3
Reputation: 403491
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.
Upvotes: 5