View Single Post
  #2  
Old 11-27-2018, 12:30 PM
loramin loramin is offline
Planar Protector

loramin's Avatar

Join Date: Jul 2013
Posts: 10,523
Default

Quote:
Originally Posted by maskedmelon [You must be logged in to view images. Log in or Register.]
If I am writing a piece of software and it is going to have many instances of a few different types of objects is it better to aggregate common/repetitive code for updates into a "manager" type class than having all that code replicated across hundreds or thousands of objects?

For example, if I have an object, say, a Collider and it contains a collision box and maybe a method for correcting position on collision, is it better to implement the code for detecting collisions within that object or within a separate CollisionManager class that could iterate over all active colliders to check for collisions?

I wouldn't think it would make a difference either way for speed, but it seems liek it would use a lot less memory to house all the collision code in a single instance than duplicating it across many objects.
C, C++, C#, or whatever it is you're using, isn't my wheelhouse. I'd tell you to abandon OOP entirely and take a functional approach ... but then I'm a Javascript programmer. I learned C back in college, but that was about as long ago as the original classic EQ.

Even so, this sounds wrong:

Quote:
Originally Posted by maskedmelon [You must be logged in to view images. Log in or Register.]
it seems liek it would use a lot less memory to house all the collision code in a single instance than duplicating it across many objects.
Generally the idea of classes is that their code lives in exactly one place: on the class. It's not like every class's methods gets copied with every instance you create. Instead, all your instances just have a single pointer back to the class, and it doesn't matter whether that class has one or a thousand methods. In other words, no matter how many instances you create, there is no duplication, only new pointers to the same existing code.

But again, not a C guy, so maybe it works differently? I'd just be surprised if it did.

If my understanding is correct, this is really more a code organization/architectural issue than a memory optimization one, and you should choose whichever approach best fits your application.
__________________

Loramin Frostseer <Anonymous>, Hetch<Anonymous>, Tecla <Kingdom>, ...
Check out the "Where To Go For XP/For Treasure?" Guides

Anyone can improve the wiki! If you are new to the Blue or Green servers, you can improve the wiki to earn a "welcome package" of platinum and/or gear! Send me a forum message for details.
Last edited by loramin; 11-27-2018 at 12:35 PM..