I'm working on a project for visually tracking trains. I've refactored the object relationship between the trains, the tracks, and the respective drawing classes for each several times, and I'm not happy with any of them. Maybe you guys can help me out here.
Here are the interfaces to the objects I've got now.
Tracks:
- addControlPoint(IntPoint point)
- getPointAtPosition(float positionOnTheCurve)
- getTangentAtPosition(float positionOnTheCurve)
- getMaxPosition()
TrackDrawer:
- setGraphicsContext(Graphics g);
- draw(Tracks track);
Train:
- set/getTrack(Tracks track)
- set/getTrackPosition(float position)
- set/getSpeed(int speed)
- moveForward(elapsedTime)
TrainDrawer:
- setGraphicsContext(Graphics g)
- draw(Train train)
And here's how they're being used at the moment.
init() {
Tracks track = new Tracks();
track.addPoint( point1 );
track.addPoint( point2 );
track.addPoint( point3 );
Train train = new Train();
train.setTrack(track);
train.setPosition(0);
}
run() {
while (running) {
elapsedTime = calcTime();
update(elapsedTime);
Graphics g = getGraphics();
draw(g);
}
}
update(long elapsedTime) {
train.setSpeed(10);
train.moveForward(elapsedTime);
}
draw(Graphics g) {
trackDrawer.draw(track);
trainDrawer.draw(train);
}
Here are a couple of specific questions:
Does it make sense that a train depends on a track? I think it does, as a train without a track makes little sense in the real world.
In order to draw the train, I need to find out the direction the train is facing on the tracks by calculating a tangent on the track itself. This causes a dependency of Track inside of TrainDrawer, which I don't care for. However, I don't think it necessarily makes sense to make Train responsible for calculating the directional vector (tangent) when it's ONLY needed for drawing the train, and for no other reason.
In general, what is the best practice for creating objects that need to be drawn in some fashion? Many people are telling me that the train should know how to draw itself, but that seems to put too many dependencies on the train itself. It doesn't feel right.