When implementing a class, there's a distinction between fields that are there because they are part of what the object is and fields that are there because they are needed to implement the object.
For example, if you are implementing an ArrayList, you will have an array that isn't part of what an ArrayList is but are required into the implementation. That's the whole point of having public properties and private fields.
The problematic case
However, I'm in a kind of weird situation were the implementation is outside the class because the algorithm is interacting with a collection of that Object.
My case is a physics engine. My Body class has public properties that make sense like Velocity, Position, Size etc. However, now it starts to have properties that only make sense when used by the class World, that interacts with a collection of bodies. I have fields like CollisionShifting (how much the body is shifted by collision), Collisions, Movement (velocity * delta, needed to check for collisions) etc. and the list is growing.
The problem is the more features I implement into World, the more unintuitive fields I have to add to Body. And that's bad because anyone who want to implement the Body interface will have to implement those fields and won't know what's their purpose. As someone who implements Body (and not World), all you care about is where is your Body and how fast does it does. You also want to get notified when collisions happen but you don't want to have details like how much the body was shifted by collisions in the last frame as a property.
So what I would like to do is having private fields per Body for the implementation of the World into the class World but I do not like the first look of this option.
I could map every Body to a class that's only purpose is to complete body into World:
Map<Body, HiddenFieldsOutOfTheModel> extraInfos;
I could do that for every field. It would be a bit more logic because I wouldn't have a weird out-of-the-model class but I feel it's just like moving the problem:
Map<Body, Vector> collisionShiftings; Map<Body, List<Collision>> collisionsPerBody; //etc.
This option is a bit tedious because whenever the World wants to remove a body, it has to remove it from a variable amount of Maps. Plus, I need to encapsulate the add/remove operations while without the Maps I can just leave write access to the array.
I have to say I prefer the second approach but is there something better ? What is the best solution to keep the model clear and confuse the less people possible?
In short, what is the best way to have per-object private fields in a class containing a collection of these objects?
It's been a while since I asked this question but I still don't have a "perfect solution". (And I'm not sure I will ever find one) For now I've taken the
Map<Body, HiddenFieldsOutOfTheModel> extraInfos; option and named my