Alternative to singleton

Started by
64 comments, last by ApochPiQ 12 years ago

It is mathematically identical in pretty well all respects


Yes it is - as per those Turing equivalence theorems, all code is identical, regardless of language or algorithm.
[/quote]
I don't see how this relates to our discussion.



void Gun::Update(bool triggerActive)
{
if(triggerActive)
{
Fire();
}
}


Enter IOC/DI...
class SceneEntity {
GlobalFlags * pFlags;
}

class Gun : SceneEntity {
...
void update() {
if (pFlags->triggerActive) Fire;
}
}
The pFlags gets passed in during construction, most likely inside a factory.
[/quote]
This illustrates what I mean. Passing in a set of global flags to a Gun class, so the Gun class now has a member pointer to a global variable, provides little or no benefit to your program. The core problem, the fact you're accessing global state in an inappropriate context (as opposed to 'inappropriate manner', by accessing that information through globals), has not been resolved


Most notably, it changes precisely nil in terms of thread safety when we're talking about references to global data, an often harped on about benefit of not using globals (which I've yet to understand)


Well:class DistributedSim {
threadlocal<GlobalFlags> flags;
Gun * g;
DistributedSim() {
g = new Gun(flags);
}
}

Now I can run multiple simulations inside same process without any lock contention.
[/quote]
You could have achieved the same outcome using threadlocal<GlobalFlags> g_flags. I'm not advising that's the way to go, I'm just saying eliminating the global instance does not, in and of itself, fix any multithreading issues. It merely prepares the framework to allow multithreaded behavior, but without actual changes to implement the thread safety, nothing changes.


Finally, IT industry concluded in 2002 that singletons spell death of project maintainability. And IT isn't known for being at bleeding edge of experimentation. Computational performance was never relevant to them (except for that certain bug they found in VMs), but performance and scalability of development was. During the next year, everything shifted to IOC/DI container or dynamic languages. None of this can be proven mathematically, but falls under soft observations. It just works better in practice.

Unsure what you mean by 'IT industry', however in terms of such a conclusion, at best, they could conclude poor use of singletons is a significant barrier to project maintainability. At worst, such a statement is an outright falsity, as some singletons never need to be removed, thus endure the complete project lifecycle.
Advertisement
Use what works for you.
a singleton in mostly every os of today: the mouse.

if it wouldn't be, we could plug in more than one mouse and use more than one cursor (similar to multitouch) since years in every os. and heck yes, i would. sadly, at some point we got limited to only one device ever, as no one ever needs more than one cursor. and the whole os' got designed around it.

now that we can have multitouch, the os' are still not designed for it (at least win7). the result: i can not, for example, drag multiple windows independently around the desktop at the same time.


it's a simple example of potential that was there since years and never got exploited just because we where all fixed on "there can be only one".
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud


a singleton in mostly every os of today: the mouse.

if it wouldn't be, we could plug in more than one mouse and use more than one cursor (similar to multitouch) since years in every os. and heck yes, i would. sadly, at some point we got limited to only one device ever, as no one ever needs more than one cursor. and the whole os' got designed around it.

now that we can have multitouch, the os' are still not designed for it (at least win7). the result: i can not, for example, drag multiple windows independently around the desktop at the same time.

it's a simple example of potential that was there since years and never got exploited just because we where all fixed on "there can be only one".


Even though I don't like singletons particularly, this is a pretty good example of that you can't predict _everything_, requirements change.
And also a pretty contrived example.

Its a pretty fringe case to have multiple mice connected (how often do you really need to move two windows independently at the same time?)
Also, with custom drivers, its no problem to connect more mice if you should need it for your special case. (controlling your game, or navigating 3d space in some editor or whatnot). The OS doesn't support it in the main UI, because it doesn't help the user experience.

Having multiple cursors isn't really similar to multitouch, multitouch is fairly more complex to handle then that, mainly because you don't know where the "cursor" is until the user touches. Even single touch is different from using a mouse.

So supporting multiple mouses and maintaining support without ever using it for the 35 years we have had mouses, would be a huge waste of time, and wouldn't really help the multi-touch case.
Every OS with multi-touch support define a new, separate API, for the multi touch case.

Though, on the other hand, the fact you can't predict everything, and that singletons is a less flexible design, is a good argument for not using singletons :)
If you ever used a 3d app like 3d studiomax, or photoshop, you would have loved more than one mouse.

The fact that you can't even imagine the possibilities (pinch to zoom with something as accurate as a mouse? a dream. same for rotation and stuff, 3d modelling in general) just shows how far a singleton can restricts ones mind.

the example i gave in an older post was about rendering to multiple contexts in the early days of opengl to do rendertotexture style stuff. as opengl is essentially a huge singleton, i never could grasp myself around it, i was too used to that "statemachine that just does everything for me". and that's exactly what singletons evolve into: huge "everythings" that then make you go blind on possibilities you're missing.


the other example (i think in the 'are globals bad' topic) was the player singleton. a lot do that, imagining there's just one player. but even in a singleplayer, that's not true. you have enemies. imagine them being able to use the same physics, the same movements, the same weapons, the same cars and planes and stuff that you do? they are a player, just one with a different "controller" behind it.

once you move away from the player singleton, suddenly you can be anything in the game, so you could start to mind-control other characters, etc. or they could use your vehicle when you don't look for it. suddenly, the game evolves while you develop it into something much bigger.

singletons always block you at the most important part: where you're thinking. because while coding, that part is active: your brain. and if the code does not allow you to do stuff, you won't even imagine that stuff anymore.



and yes, touch and mouse are not the same. hence multitouch does not replace the power of multiple mice. i feel this the most when i'm producing music. to do it accurately, you can't use touch. you need a mouse. but more than one mouse would allow so many features we're used from multitouch, it would be great.
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Even though I don't like singletons particularly, this is a pretty good example of that you can't predict _everything_, requirements change.[/quote]

/dev/mouse1, /dev/mouse2, /dev/mouse3, ..., /dev/mouseN. Same for any other hardware resource.

It's "one mouse should be enough for everyone" that is unusual.
obviously, there are multiple mice supported (at least, since usb mice exist. not sure about os' that only had support for ps2 connections). but not multiple cursors. at least, not by default (i've seen special drivers, and one can use those for apps, but they a) don't follow the mouse speed settings of the os and b) don't support the rest of the os, which is, as said, designed for singleton szenarios).

no clue about linuxs situation, of course.. i'm a windows guy.
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

I have multiple mice plugged into my computer, I have my wireless mouse, my laptop trackpad and my wacom tablet. It's technically the pointer that would be the singleton.



I'm kinda shocked at the lack of design pattern proponents in this thread. The Service Locator pattern, and/or Dependency injection are effective means of decoupling a globally available resource in a manner that is well abstracted, while still unit-testable. Although I suppose passing the interface in via parameter is an example of DI of sorts. Design patterns are your friend, as these software development problems are the same regardless to if you are writing a Forex trading system or a 3D renderer.


I'm kinda shocked at the lack of design pattern proponents in this thread.


Mildly blind obedience to design patterns is a significant reason that singleton overuse exists in the first place.

[quote name='Serapth' timestamp='1332864138' post='4925714']
I'm kinda shocked at the lack of design pattern proponents in this thread.


Mildly blind obedience to design patterns is a significant reason that singleton overuse exists in the first place.
[/quote]


Yes, but ignoring them is throwing the baby out with the bathwater.

This topic is closed to new replies.

Advertisement