mercredi 25 mai 2016

Refactoring a large class into smaller classes

Hey so I'm trying to get into better coding habits after a large project of mine became somewhat unmanageable. I've started taking particular care in trying to design my classes after the SOLID principles as well as applying design patterns where needed. I've come to a bit of a fork in the road when trying to refactor a large class of mine however.

Basically the class controls behavior for a camera in a 3D game. It has a gameobject which allows me to modify it's position and rotation among other things. My problem is I have methods for several different behavioral aspects as well as fields for each behavior.For example: Adjust tilt to ground level, adjust height to ground level, smooth position, smooth rotation, smooth tilt, follow object, lock to position etc. Point being, there are a bunch of "behaviors". The amount of fields alone has become a big wall at the start of my code which I don't like very much.

I have moved some of these methods and fields to separate classes and have included references to their objects in my camera class. I have also created an interface for behaviors. So now my Camera calls UpdateBehavior on the various different objects and passes an instance of itself in as a parameter. The behavior class then operates on the camera and updates its position, tilt or rotation.

My question is, is this too tightly coupled? The camera basically calls another object to operate on itself. Is that ok? Also where do I draw the line at what is relevant in each class. I could potentially create quite a few classes based on this one class alone if I break everything up. Is this the conventional way of doing things?

Or am I being too over the top by doing this. The class itself would be about 1.5k lines long. The only responsibility it has is to move the camera in a specific way. It just does this through a bunch of different methods and I'm not sure if I should be considering tilting a separate responsibility for example.

By doing this it has helped me develop the various specific areas. Where before I'd be looking around the code trying to figure out what is associated with what exactly. Where now it is quite clear.

Any input would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire