Alternatives to singletons for data manager?

Started by
30 comments, last by _the_phantom_ 10 years, 11 months ago
I USED TO use a lot of global variables and singletons in my code until I started expanding my functionality and noticed that many things that I thought that I had only needed one of, I actually needed more. Even some "managers" and "systems" worked better and was more intuitive with more than one instance. Sometimes this was because of resource or system lifetimes and some other reasons that I can't think of. (I'm typing this on my phone while eating dinner. ) I found that passing references to managers and factories, as well as having some resources keep a pointer to their managers made things much nicer. Globals and singletons looked nice at first but in the end it made things more difficult. Later when I'm on my computer, I may post some examples. (If I remember)

Learn all about my current projects and watch some of the game development videos that I've made.

Squared Programming Home

New Personal Journal

Advertisement

One of the clever, but more contraversial alternatives I've found for sharing data between classes is to use inheritance/static fields. Let's say you're programming a 3D editor of some sort, and a list of vertexes in your model has to be shared between many classes. The options in this thread include simply passing the list as a parameter, setting up a singleton, or setting up a container class that gets passed at runtime.

With my inheritance method, you set up an abstract class that contains protected static fields for shared information - such as the list of vertexes that has to be shared. Any class that needs access to those vertexes would inherit the abstract class.

This can be extended into a tree of inheritance to allow different child classes to have different read/write permissions on the abstract class' fields, but I would restrain myself from doing this too much at risk of ending up with 3-4 depth inheritance trees.

The most significant issue with this model is that without the proper visualization tools, maintaining code that runs under this system can be very complicated, especially if you have poor docs. In addition, if someone else was to read your code without knowing what you're doing - it's likely they would get very confused very fast. If you use this system extensively, I suggest writing an addon/macro for your development environment that lets you inspect what kind of inherited fields each class has without actually opening up the abstract class.

One of the clever, but more contraversial alternatives I've found for sharing data between classes is to use inheritance/static fields. Let's say you're programming a 3D editor of some sort, and a list of vertexes in your model has to be shared between many classes. The options in this thread include simply passing the list as a parameter, setting up a singleton, or setting up a container class that gets passed at runtime.

With my inheritance method, you set up an abstract class that contains protected static fields for shared information - such as the list of vertexes that has to be shared. Any class that needs access to those vertexes would inherit the abstract class.

This can be extended into a tree of inheritance to allow different child classes to have different read/write permissions on the abstract class' fields, but I would restrain myself from doing this too much at risk of ending up with 3-4 depth inheritance trees.

The most significant issue with this model is that without the proper visualization tools, maintaining code that runs under this system can be very complicated, especially if you have poor docs. In addition, if someone else was to read your code without knowing what you're doing - it's likely they would get very confused very fast. If you use this system extensively, I suggest writing an addon/macro for your development environment that lets you inspect what kind of inherited fields each class has without actually opening up the abstract class.

I'd really recommend against doing that.

You're abusing all kinds of mechanisms there.

if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

I USED TO use a lot of global variables and singletons in my code until I started expanding my functionality and noticed that many things that I thought that I had only needed on of, I actually needed more. Even some "managers" and "systems" worked better and was more intuitive with more than one instance. Sometimes this was because of resource or system lifetimes and some other reasons that I can't think of. (I'm typing this on my phone while eating dinner. ) I found that passing references to managers and factories, as well as having some resources keep a pointer to their managers made the could much nicer. Globals and singletons looked nice at first but in the end it made things more difficult. Later when I'm on my computer, I may post some examples. (If I remember)

This, basically. If you need one of something there's a chance that at some stage you're going to need more than one - even if you think you don't. Why constrain yourself? Just make it a regular class now and only instantiate once if one is all you need right now - that way if (when!) the time comes when you need more than one you'll be able to have more than one without major hassle.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

I'd really recommend against doing that.

You're abusing all kinds of mechanisms there.

I'd have to agree here. At the very least you're doing exactly what was pointed out to be a major negative of globals, you don't know what dependencies an object has. Using a base class like that could appear to the child classes to be not much better than a global or a singleton. There's a lot of little problems with it.

One of the clever, but more contraversial alternatives I've found for sharing data between classes is to use inheritance/static fields. Let's say you're programming a 3D editor of some sort, and a list of vertexes in your model has to be shared between many classes. The options in this thread include simply passing the list as a parameter, setting up a singleton, or setting up a container class that gets passed at runtime.

With my inheritance method, you set up an abstract class that contains protected static fields for shared information - such as the list of vertexes that has to be shared. Any class that needs access to those vertexes would inherit the abstract class.


I know you've put a lot of thought into your way of avoiding globals, but your editor example is a good example for passing references. At first many may think, I have this model editor and it can only do one model at a time. I've made a model editor and made the same assumption. Sure if that's true, just use a global or singleton rather than a more complex organization just to avoid using globals.

But really, will an editor only store ONE model? Maybe this editor allows you to load a character model, but it can also load attachments like weapons. The editor can manipulate the character and the attachments. It seemed like only one model was needed, but after adding more functionality, now you need more than one. So when you open a dialog or tool in the editor, the dialog or tool will need to be passed a reference of what it will manipulate.

Learn all about my current projects and watch some of the game development videos that I've made.

Squared Programming Home

New Personal Journal

A basic philosophy I sort of follow is that code should be (in order):

  1. As optimized as necessary.
  2. Simple
  3. Transparent

Singletons usually fail on the transparency case, but they're worth using if the alternative has huge code complexity costs. Dependency injection is fine for a manager type of thing, but despite what I used to think, there are cases where globals/singletons have use. Logging is the canonical example. For example, I have a fractal library with deeply nested trees of noise/fractal modules. If I want to enable logging on those modules, I can do one of three things:

  • Pass a logger as a depdency to every module (nearly my entire library -- 30+ classes).
  • pass a parent reference to propagate a logging message to the root, which handles logging itself (isn't this just isomorphic to throwing an exception and catching at root?)
  • just call a stinking logger global.

Dependenccy injection is "ugly" by some metrics, and it's simply too ugly for this use case. Ergo, I use a singleton.

Why don't you just use free functions (or, if in a language that doesn't support them, statics)?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

One of the clever, but more contraversial alternatives I've found for sharing data between classes is to use inheritance/static fields. Let's say you're programming a 3D editor of some sort, and a list of vertexes in your model has to be shared between many classes. The options in this thread include simply passing the list as a parameter, setting up a singleton, or setting up a container class that gets passed at runtime.

With my inheritance method, you set up an abstract class that contains protected static fields for shared information - such as the list of vertexes that has to be shared. Any class that needs access to those vertexes would inherit the abstract class.


I know you've put a lot of thought into your way of avoiding globals, but your editor example is a good example for passing references. At first many may think, I have this model editor and it can only do one model at a time. I've made a model editor and made the same assumption. Sure if that's true, just use a global or singleton rather than a more complex organization just to avoid using globals.

But really, will an editor only store ONE model? Maybe this editor allows you to load a character model, but it can also load attachments like weapons. The editor can manipulate the character and the attachments. It seemed like only one model was needed, but after adding more functionality, now you need more than one. So when you open a dialog or tool in the editor, the dialog or tool will need to be passed a reference of what it will manipulate.

I'll admit my example really wasn't that great, but inheritance based global access isn't around to replace something like a class full of references.

An infinitely better example would be an static-inheritance class giving access to something like the graphics device. On one of my older projects, the "sprite" class is typically accessed very far down the call stack. From the entrypoint of the program, you have to go through a few layers of gamestate logic, then UI logic, then all the way at the bottom is the sprite class. The sprite class needs access the graphics device, or whatever class is handling the graphics device. Instead of passing down a reference to the graphics device down a dozen layers of call stack, the Sprite class would inherit from a static class that contains the graphics device.

This way, access to the graphics device has to be explicitly defined through the use of inheritance, and any usage of the graphics device can be found by just looking at all the child classes that inherit from it. In addition, it's one less piece of trash floating around the global namespace.

I'll admit it's hacky as hell, but I'd rather have an ugly hack than restructure 10k lines of UI logic.

A basic philosophy I sort of follow is that code should be (in order):


  • As optimized as necessary.
  • Simple
  • Transparent
Singletons usually fail on the transparency case, but they're worth using if the alternative has huge code complexity costs.


a global instance of a normal class is less complex than a singleton and more flexible.

Singletons are just a global with a "there can be only one" restriction and some makeup, If you don't absolutely need to restrict the number of instances to 1, just use a global(put it in a sane namespace), using singletons because "globals are evil" is silly since a singleton is a global. (and globals are only really evil if their state can be modified globally, stateless functions, constants, etc are perfectly fine to allow global access to).
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement