4 ms·
The OO solution is to have a PhysicalObject class that dog and car both inherit from (or use via composition).
by jppittma 3y ago
The OO solution is to have a PhysicalObject class that dog and car both inherit from (or use via composition).
- mrkeen 3y agoThen there's even more scattering around of the logic. Good way: PhysicsEngine { List<Entities> entities; doCollisions() { // Logic involving instanceof } } Bad way: PhysicsEngine { List<Entities> entities; doCollisions() { // Delegate to whomever. entities.forEach(e -> e.collide()); } } Dog { collide() { // What the hell can I do here? // I don't know about the rest of the world } } Car { collide() { // What the hell can I do here? // I don't know about the rest of the world } } Worse way: PhysicsEngine { List<Entities> entities; doCollisions() { // Delegate to whomever. entities.forEach(e -> e.collide()); } } Dog : PhysicalObject { collide() { super.collide(); } } Car : PhysicalObject { collide() { super.collide(); } } PhysicalObject { collide() { // Not only do I not know about the rest of the world // I don't even know how *I* collide, because what am I? } }