I have an abstract class with a few inheriting classes that represent shapes and I would like to test for collisions. For now, here is my (Java) code:
public abstract class Shape {
public abstract boolean collision(Rectangle rectangle);
public abstract boolean collision(Circle circle);
}
public class Rectangle extends Shape {
// implementation of both abstract methods
}
public class Circle extends Shape {
// implementation of both abstract methods
}
I see the flaw in my code: I have to modify every class if I want to add another child class, say Triangle
.
I still would like to be certain that every collision is possible (Rectangle-Rectangle, Circle-Circle, Rectangle-Circle and Circle-Rectangle, plus the 5 more that Triangle would introduce, and so on).
Is there a design pattern that I haven't thought of that solves that problem nicely?
Aucun commentaire:
Enregistrer un commentaire