This question has been bothering me for quite a while:
I have a Room class, and lets say I want to add a Ball entity.
Ball should have x and y coordinates that represents a location in this room.
So here is the question:
What class should hold the coordinates?
1) Each Ball for it self.
2) The Room will hold the coordinates of every Ball.
I know that it’s probably doesn’t matter much, but what is preferred choice or what is more popular?
EDIT:
What I’m actually worried about, is that object can change it’s content however it like, so having it limited by some third party would solve it. But I guess it’s only valid for applications with multiple users, not my case.
When it comes to object design, one of my starting mantras is “the object knows everything about itself“. Imagine the Ball having to ask the Room – where am I? I would start with the Ball holding it’s coordinates. This way the Room only has to know about itself and a collection of Ball objects. Conversely the Ball knows everything about itself. Any details the Room may need about Ball coordinates can be gathered by iterating the the collection. When you get around to adding other objects to your room, you’ll impact your design a lot less as well.