5 ms·
Better naming pattern: SingletonAntipattern*.
by CmonDev 12y ago
Better naming pattern: SingletonAntipattern*.
- Mithaldu 12y agoIn libraries and many kinds of software, yes, especially on the web. However in games it can often be quite reasonable to use singletons. He gives a great example with TheVoip. You're extremely unlikely to have a game that actually needs to handle multiple voip connections at the same time, nevermind voice inputs.
- eddd 12y agoSingletons in multicore environments are dangerous, one bottleneck to rule them all.
- ryanthejuggler 12y agoAlso, singletons are great in robotics control software. It removes a lot of complexity if you have one singleton corresponding to one device.
- wnoise 12y agoWhat happens if you add a second camera? The actor/CSP model makes each device a separate process, which can be hooked together with processing nodes works wonderfully.
- meepmorp 12y ago> What happens if you add a second camera? You call it TheSecondCamera, duh.
- sp332 12y agoTheCameraArray[1]
- ryanthejuggler 12y agoIf there's the potential for multiples then I'll have a CameraManager (TheCameraManager) that finds out what's connected to the system and provides enumeration and access. I'll generally try to make the cameras look like a singleton and hide the Camera class. Sort of like a factory, except you'll only ever get N instances of your Camera class for N cameras.
- midnightclubbed 12y agoUntil you port your multiplayer game to the Xbox One where each controller has it's own mic/headset.
- Zikes 12y agoI would imagine John Carmack ought to know how to handle and prepare for videogame ports by now.
- midnightclubbed 12y agoI'm sure he does, his games have been ported to a good number of platforms! That said he tends to be very PC-centric and in pushing that platform makes decisions that make for difficult ports (eg megetextures). For programmers who have not been through the process of evolving game design and engine requirements getting the balance of flexibility and productivity is tricky. Hoping the fact JC uses singletons does encourage them to be sprinkled more liberally than they already are!
- mbrock 12y agoSo then you change the code. Or make the singleton handle several things.
- midnightclubbed 12y agoYeah. Trade off is whether changing the code (which may have been referenced everywhere in the codebase) every time this happens cost more (in dev and debugging time) than not using the singleton in the first case. It's things like this that turn a 'sure, we can make this game split-screen' into 'you didnt tell us you wanted split screen, that's gonna take a while'.
- mbrock 12y agoWell, this is very hypothetical. I go by "you aren't gonna need it" and "do the simplest thing that could possibly work." Maybe I'll have to change a bunch of stuff when someone suddenly comes up with a split-screen requirement. I probably will have to change a bunch of stuff anyway. As long as the code isn't impossible to understand and change, I would in most cases prefer to add split-screen support once I know for sure I need it.
- qwer 12y agoThat still doesn't explain how the singleton is better / easier. It just explains why it's not that bad.
- vkjv 12y agoIMHO, like most patterns, the pattern is appropriate in some situations and not always an anti-pattern. It only becomes an anti-pattern when it is abused in situations where it is not appropriate. For example, how would you go about implementing a connection pool without a singleton? Even if you pass it around everywhere, it's still a singleton, just explicit instead of implicit.
- chris_wot 12y agoWhat if you have two types of connection pools?
- nsxwolf 12y agoThen you have a singleton containing singleton pools.
- dpark 12y agoThe problem (at least one of the biggest problems) with singletons is exactly that the singleton state is explicit. Making a singleton explicit means making it global, which allows random code all over the place to take direct dependencies on it. This increases coupling, and also makes it hell when you realize you were wrong and you need two instances of the singleton. To your example of the connection pool, there is absolutely no reason that this has to be a singleton. You could have N connection pools, each with their own pool of connections, just as you could have N thread pools, or N memory heaps. This is not only possible but sometimes preferable, if different subcomponents need resource isolation (so that some connection-hungry component can't starve a component that needs reasonable responsiveness for the interface, for example). Further, most of your code shouldn't even care about the connection pool. That's a level of coupling you generally do not need. Most code should care only about an actual connection, which could be passed in. Taking a direct dependency on the connection pool when not necessary increases pointless coupling and makes changes more expensive.
- TheCoelacanth 12y agoA class that happens to only ever be instantiated once is not automatically a singleton. It is only a singleton if it takes steps to ensure that it can never be instantiated more than once. I don't see any reason that a connection pool would have to make sure that there are no other connection pools in the application.
- raverbashing 12y agoSingletons are how global variables are named to pretend they're ok to abuse. (Don't get me wrong, they are necessary)