views:

103

answers:

2

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:

  1. 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.

  2. 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.

  3. 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.

+2  A: 
  1. If your entities contain more logic than only being drawn, I would not relate train directly on track. How about the speed of the train dependent on the current track? I'd put some Tour in between. This makes it easy to abstract and provide different behaving implementations if needed.
  2. The direction is already the first information which does not really an information of a train. This is good placed in the tour.
  3. Good job! Train is definitely not responsible to draw itself. If you ever have to render your train to another device or need the possibility to filter some details you get into trouble.

Greets Flo

Florian Reischl
Unfortunately, I don't get your meaning with the "Tour". Any chance you can explain that in a little more detail?
hokiecsgrad
+2  A: 

My first impression is that TrainDrawer and TrackDrawer are the source of the problem and should not exist.

Following the model-view approach, Trains and Tracks are model entities, neither of which has the other. If you consider a Draftsman object (corresponding to the "view"), the draftsman gathers information from Tracks and Trains and renders a coherent whole.

msw