Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Alternatives to singletons for data manager?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
31 replies to this topic

#21 Squared'D   Members   -  Reputation: 2255

Like
1Likes
Like

Posted 08 May 2013 - 03:36 AM

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

 


Sponsor:

#22 zalzane   Members   -  Reputation: 191

Like
0Likes
Like

Posted 08 May 2013 - 03:48 PM

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.


Edited by zalzane, 08 May 2013 - 03:49 PM.


#23 ChaosEngine   Crossbones+   -  Reputation: 2499

Like
0Likes
Like

Posted 08 May 2013 - 04:23 PM

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

#24 mhagain   Crossbones+   -  Reputation: 8269

Like
0Likes
Like

Posted 08 May 2013 - 04:29 PM

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.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#25 Satharis   Members   -  Reputation: 1249

Like
1Likes
Like

Posted 08 May 2013 - 11:28 PM

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.

#26 Squared'D   Members   -  Reputation: 2255

Like
0Likes
Like

Posted 09 May 2013 - 08:12 AM

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

 


#27 SeraphLance   Members   -  Reputation: 1452

Like
0Likes
Like

Posted 09 May 2013 - 11:23 AM

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.



#28 ApochPiQ   Moderators   -  Reputation: 16377

Like
1Likes
Like

Posted 09 May 2013 - 12:15 PM

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

#29 zalzane   Members   -  Reputation: 191

Like
0Likes
Like

Posted 09 May 2013 - 12:40 PM

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.



#30 SimonForsman   Crossbones+   -  Reputation: 6288

Like
1Likes
Like

Posted 09 May 2013 - 12:53 PM

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).

Edited by SimonForsman, 09 May 2013 - 01:01 PM.

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!

#31 Juliean   GDNet+   -  Reputation: 2719

Like
0Likes
Like

Posted 09 May 2013 - 12:59 PM

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 appears to be more a flaw of your overall design. Normally, the further down you go, the less you information you need - and the less things you have to pass on. Sure there is exceptions to this, but if you really need to acces the graphics device down so far that you have to pass it down several layers without using it, there is something horribly flawed in the first place. Your classes probably have too much responsilibity, aren't encapsulated well enough, etc... . I remember the same thing from one of my first projects, where I had to pass the DX9-device around EVERYWHERE. But this was, also, a design flaw per se and not a inconvieniente thing solved by inheriting from a class that grants access to a graphics device.

 

Seriously though, this inheritance-thingy is not much better than a global or singleton. In fact, it is even worse, because it create a relationship that has no purpose other than to save passing a parameter. Inheritance should always represent an "is a" relation. Whats that in your case? "sprite" is a "convienient static data container for a g.d."? That sounds about wrong.

 

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

 

Well, you better would - otherwise you'll eventually reach a point where there is no back and forth, where the code overall becomes so ugly and hacked together that you can't develope it without insane effort, and where refactoring is virtuall impossible, forcing you to start over. But after all, thats up to you - just saying that the earlier you start to rely on hacks (namely things that you yourself call "hacky") the more messed up your code gets...



#32 phantom   Moderators   -  Reputation: 7553

Like
3Likes
Like

Posted 09 May 2013 - 01:04 PM

An infinitely better example would be an static-inheritance class giving access to something like the graphics device.

No, that would be another example of a massive design flaw...




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS