Jump to content
  • Advertisement
Sign in to follow this  
nsto119

Some questions about proper OO design.

This topic is 3789 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there any reason whatsoever to create a class for an object you'd only ever want one instance of? Say, for example, I had a.... ResourceManager class. There's no reason I'd ever want more than one of those (okay, so that's probably not entirely true, but that's beside the point). Is there really any compelling reason to make that a class instead of just a collection of functions? It's possible and easy to hide the data the ResourceManager needs without using a class. I'm aware of the Singleton design pattern, but that doesn't really seem to offer any advantages either.

Share this post


Link to post
Share on other sites
Advertisement
Making it a class will allow you to much more readily control which code is using it. Any code can call a free or public static member function - the function is effectively 'global' - so if you want to change that function, it's harder to predict what the effects are going to be.

If, on the other hand, it's a non-static member function, then only code that has been passed the instance of the class will be able to call it. The function is not global; you're explicitly passing around a "token" that allows code to call those functions, and you can keep control of it accordingly.

Share this post


Link to post
Share on other sites
Quote:
Original post by nsto119
Is there any reason whatsoever to create a class for an object you'd only ever want one instance of?

Say, for example, I had a.... ResourceManager class. There's no reason I'd ever want more than one of those (okay, so that's probably not entirely true, but that's beside the point). Is there really any compelling reason to make that a class instead of just a collection of functions? It's possible and easy to hide the data the ResourceManager needs without using a class.


Strange, a post where someone doesn't see the merits of Singletons. Surprising...


Free functions are fine. They don't however allow multiple instantiations. They also don't provide you with encapsulation, so people can alter the internal state. A class at least provides ability to hide that.

Your ResourceManager might only be one. But you'll also need a FooManager and BarManager and ....

If you choose to go with free functions, you'll find yourself unable to re-use perfectly good code.

A class also gives you options to change the behavior later, or expand on it, such as add TextureManager, PlayerManager, both of which share most of same behavior.

Share this post


Link to post
Share on other sites
Quote:
Original post by superpig
Making it a class will allow you to much more readily control which code is using it. Any code can call a free or public static member function - the function is effectively 'global' - so if you want to change that function, it's harder to predict what the effects are going to be.

If, on the other hand, it's a non-static member function, then only code that has been passed the instance of the class will be able to call it. The function is not global; you're explicitly passing around a "token" that allows code to call those functions, and you can keep control of it accordingly.


That might be true, but there's really nothing stopping anything from creating a new instance of the class either, which might be useless, but I suppose could be harmful in some situations.


Sometimes there ARE functions that everything needs access to. Classes remove this possibility unless you want to start using globals.

Share this post


Link to post
Share on other sites
Quote:
Original post by nsto119


Sometimes there ARE functions that everything needs access to. Classes remove this possibility unless you want to start using globals.


Static free functions *are* globals.

Non-global functions cannot hold state.

Share this post


Link to post
Share on other sites
Quote:
Original post by nsto119
That might be true, but there's really nothing stopping anything from creating a new instance of the class either, which might be useless, but I suppose could be harmful in some situations.
There are ways of preventing that separately - the most obvious one would be to use a class factory that keeps track of the number of instances of the class that have been created, and refuses to create new ones beyond that limit. Something like this:


class MyRestrictedObject
{
protected: MyRestrictedObject() { /* ... */ }

public: static MyRestrictedObject* CreateInstance()
{
static int numInstances = 0;
if(numInstances >= 1) return NULL; // or throw an AlreadyCreatedException
++numInstances;
return new MyRestrictedObject();
}
};







Quote:
Sometimes there ARE functions that everything needs access to.
Yes, but they're a lot less common than you'd think. I frequently see people claim that they need things like a ResourceManager to be global, but that's really not true - only code that is actually working with resources (for example, your renderer, sound system, etc) really needs access. Your physics code or gameplay code generally does not.

Which specific modules are you considering this for, anyway?

Quote:
Classes remove this possibility unless you want to start using globals.
Or singletons. This is true, but it's a box you can "break open" later - for example, by turning the class into a singleton; all your existing code that passes the object around will still continue to work happily. The inverse - unstitching a module from the globally accessible space - is harder, because you've got to visit every site where the module is referenced and figure out how you're going to get the object reference to that site.

Share this post


Link to post
Share on other sites
Quote:
Original post by nsto119
Is there any reason whatsoever to create a class for an object you'd only ever want one instance of?

1. You can't be sure what happens in the future
2. Where is the harm in making something a class and create just one instance (for now)?
3. Inheritance and Polymorphism only work with classes
4. Globals are the devil

Quote:
Original post by nsto119
I'm aware of the Singleton design pattern

5. The Singleton is the devil disguised with deceiving OO-sunglasses to lure you into chaos

Share this post


Link to post
Share on other sites
Quote:
Original post by superpig
Which specific modules are you considering this for, anyway?


Basically just a Quake style console. I need just about everything to access it for debug output.

Share this post


Link to post
Share on other sites
@ superpig:


public: static MyRestrictedObject* CreateInstance()
{
static int numInstances = 0;
if(numInstances >= 1) return NULL; // or throw an AlreadyCreatedException
++numInstances;
return new MyRestrictedObject();
}




Isn't numInstances = 0 in the wrong place? Won't this just set numInstances to 0, fail the if statement, increment numInstances to 1, and return a new MyRestrictedObject() every time?

Share this post


Link to post
Share on other sites
Quote:
Original post by Shakedown
@ superpig:

*** Source Snippet Removed ***


Isn't numInstances = 0 in the wrong place? Won't this just set numInstances to 0, fail the if statement, increment numInstances to 1, and return a new MyRestrictedObject() every time?

Local static variables are initialized on first use only, and preserved between function calls.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!