For the following model, would you prefer inheritance or composition:
- I want to draw objects on a
canvas
that each represent a data object - think of it like a state machine diagram:
Ellipses
representStates
,Lines
representconnections/transitions
between them. The object representation itself will never change, ie aState
will always be shown by aellipse
. But the way anellipse
is drawn should differ, eg for selection it should have a different color, while dragging it should maybe have an alpha channel, etc.
From the design point of view, an ellipse
is not a state
, and a line
is not a transition
. Anyhow it would be suitable to combine both objects for being able to collect them in a List<Shape>
and execute shape.draw()
on every object.
Now 2 design models are possible, whereas I would consider 2 classes to be always the same:
interface Shape {
void draw();
}
abstract class Figure implements Shape {
//basic vars like start and end coordinates
int x0, y0, x1, y1;
}
Inheritance:
abstract class State extends Figure {
String name;
}
class Rectangle extends State {
@Override void draw();
}
class Line extends Figure;
class Transition extends Line
Although from design point of view a rectangle
is not a State
, and a state
is not a figure
, regarding the drawing context this might be viable. Because I could inherit most of the stuff needed for handling the shapes
, drawing etc.
Or Composition:
abstract class State {
String name;
}
class Rectangle extends Figure {
private State state;
@Override void draw();
}
class Line extends Figure {
private Transition transition;
@Override void draw();
}
So Rectangle
+ Line
would be wrappers for my objects.
Should a Rectangle and Line rather extends State and Transition, or contain it?
Or, maybe there is also a 3rd design option that I did not see. Looking forward to your ideas.