Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I avoid "coupling" in OOP

Tags:

c++

oop

OK, I'm not sure coupling describes truly my problem. The problem is, I'm creating my own 3d game engine based on ogre. I'm also using a physic library, PhysX, but since I need to create an abstraction layer between my main engine code and PhysX - so just in case we could use another physic engine - I decided to create a wrapper, Quantum.

  • So ogre have an Entity class which will control the entity on the screen

  • PhysX also have a PxActor class which contains the informations about the actor position in the PhysX works.

  • The quantum wrapper will have a QEntity class which will act as a layer between the engine and PhysX

  • Finally, the engine will have an Entity class which will have two members at least, the ogre entity object and the quantum entity. On each update() it will ask the QEntity what's up and update the ogre entity position.

Spot the problem? 4 classes for one entity? And remember that we need to access all four entities at least 60 times/s! So what about data partitioning? Not really optimised. Besides, there might be more classes, one for the AI,  one for the scripting engine...

like image 992
Vinz243 Avatar asked Sep 29 '22 04:09

Vinz243


1 Answers

Using objects of multiple classes to represent the same thing in multiple contexts is not a bad thing in itself. In fact, it would probably be worse if you had used the same object in all these contexts - for example, through some creative use of multiple inheritance.

Your QEntity class already does decoupling for you - as long as you program to its interface, and does not let classes specific to PhysX "stick out" from the interface of QEntity*, you are good.

It looks like your project introduces coupling in the "bridge" classes, which is exactly where it belongs. As long as you keep it there, your design would not have a coupling problem.

As far as 60 FPS goes, don't worry about it too much at the design stage. As long as there are no lengthy chains of responsibility relying on virtual functions, your compiler should be able to do a good job optimizing it for you.

* for example, QEntity shouldn't accept parameters or return objects that are specific to PhysX, except in a constructor that creates a "wrapper".

like image 110
Sergey Kalinichenko Avatar answered Oct 04 '22 03:10

Sergey Kalinichenko