[SOLVED] Singleton Patterns - Controversy
Posted: Wed Dec 15, 2010 9:59 pm
Since finishing exams, I've gone back to my XNA game and hit my "Programming Game AI by Example" book so that I can put some AI entity's into my game (since it currently relies solely on pvp gameplay which would be prohibitive if no one else was online to play). The book seems to start pretty strongly with Singletons - something I've used quite a bit in my XNA project as well as somewhat in my Java framework. To me, so far, it's seemed like a relatively intuitive way of designing complex systems without passing crap all over the place. It gets those hard dependencies out in the open and makes them easier to track (in my limited experience) by defining a system that handles them (as opposed to stringing references through your code by passing them through one object to another object to another object... which I find stupidly hard to keep track of).
There seems to be a lot of controversy over their use, particularly in C++ (I've read less commentary pertaining to Java/C#). This thread is a good example:
http://www.gamedev.net/community/forums ... hichPage=2
One member in that thread seemed to feel that most advocates of singletons were less experienced developers who found a convenient fit to a difficult design decision, while the more experienced developers banned them completely after they became a headache. I think I can understand some of the reasons why they're not preferred. I've sat here mulling it over and reading more, and I can see some of the points that are being made. In my mind, I think the reason that it seems so intuitive is that I'm designing the system and it seems like a logical and easy to keep track of structure, but I imagine that as my focus shifts to other structures it may become less clear what I was thinking at the time I designed it. The thing is, this happens with anything but the simplest of systems anyway - and a system that revolves around a common reference point seems better, in my mind, to keep that structure tight and easy to follow when compared to dropping references here and there through out my code.
This is all off track of the task I had in mind (programming a simple AI agent so that single player is an option) when starting this book , but since I've been leaning toward such heavy use of the singleton pattern, I thought it would be a good idea to analyze that decision. It won't change my XNA project's design since aside from integrating AI, the last big task, all I need to finish is text/textures in the menu interface and HUD and potentially add leaderboards - but I may approach the framework I've been developing in Java from a different angle.
What advice/thoughts/opinions do some of the more experienced programmers here have? The thread I linked above was a good read, but there's a lot of devs there who openly admit that they are strongly against global variables in every sense, so I wonder how much bias may be clouding their judgement. Additionally, I didn't see many suggestions as to how to build a better system, other than a brief mention of creating an eventhandler.
There seems to be a lot of controversy over their use, particularly in C++ (I've read less commentary pertaining to Java/C#). This thread is a good example:
http://www.gamedev.net/community/forums ... hichPage=2
One member in that thread seemed to feel that most advocates of singletons were less experienced developers who found a convenient fit to a difficult design decision, while the more experienced developers banned them completely after they became a headache. I think I can understand some of the reasons why they're not preferred. I've sat here mulling it over and reading more, and I can see some of the points that are being made. In my mind, I think the reason that it seems so intuitive is that I'm designing the system and it seems like a logical and easy to keep track of structure, but I imagine that as my focus shifts to other structures it may become less clear what I was thinking at the time I designed it. The thing is, this happens with anything but the simplest of systems anyway - and a system that revolves around a common reference point seems better, in my mind, to keep that structure tight and easy to follow when compared to dropping references here and there through out my code.
This is all off track of the task I had in mind (programming a simple AI agent so that single player is an option) when starting this book , but since I've been leaning toward such heavy use of the singleton pattern, I thought it would be a good idea to analyze that decision. It won't change my XNA project's design since aside from integrating AI, the last big task, all I need to finish is text/textures in the menu interface and HUD and potentially add leaderboards - but I may approach the framework I've been developing in Java from a different angle.
What advice/thoughts/opinions do some of the more experienced programmers here have? The thread I linked above was a good read, but there's a lot of devs there who openly admit that they are strongly against global variables in every sense, so I wonder how much bias may be clouding their judgement. Additionally, I didn't see many suggestions as to how to build a better system, other than a brief mention of creating an eventhandler.