Interdependent Class design question

Hey guys,

I have a simple design issue. I need to figure out a way around this.

I am programming a simulated railway, which is made up of stations. Each station holds an array of passengers.

Passengers must have a function that allows them to pathfind their way from one part of the railway to another. IE: passenger at 3rd stop must go uptown to get to 5th stop, but must go downtown if going to 1st stop. Or alternatively, might need to change train lines before continuing on to final destination.

Issue here is that the Passenger class must be able to hold a list of stations, so that a passenger object can pathfind between them. But the Station class must be able to hold an array of passengers. I can't compile either, because each is calling the other.

I'm sure there are a few solutions to this, but I'd like to know which one you guys think is best.

Much thanks!
Each station holds an array of passengers.


Issue here is that the Passenger class must be able to hold a list of stations, so that a passenger object can pathfind between them. But the Station class must be able to hold an array of passengers. I can't compile either, because each is calling the other.


Definitely a design problem, I think. The problem is more complex - can't be solved with 2 classes IMO. Both the relationships you mentioned are not correct.

A completely different approach is needed IMO.

These sort of problem are usually solved with a graph of the train network. Graphs have nodes (the stations), they also have edges which connect 2 stations.

The Nodes store the name of the station, and which stations are connected to it.

The Edges store info like the two stations which define which node it is, and info such as cost, distance, time etc.

So now we need a data structure to store all this info. No matter how simple or complex the network is, you need a Graph object. Whether you call it a graph or a network is up to you

The simplest situation is a network with just one line - so you could use a linked list for this.

Next there is a main line, with other lines branching off. I am not sure whether having a tree to model the network is the best for this, or whether there is a better solution. You might be able to have a <set> (sets are sorted )of Edges. The stations could be numbered so it is easy to tell which stations are further away from the root (Central Station), or store the distance form the root as a member variable of a Station node.

The most complex model of a network is one that involves interconnected circuits.

To solve a route calculation, requires matrix calculations (so far a I remember - it was bleems ago when we looked at this very briefly at Uni)

Anyway the Route calculation should be a part of the Graph class, although the Route returned could be stored in the Passenger object.

I don't really know how to do this in detail, but hopefully I have given you some ideas to work with.

Hop all goes well.
Hey thanks a lot for the feedback!

I tried various designs to get around this issue, but wasn't able to get past the fact that I needed passengers to know what a station was, and needed stations to know what a passenger was.

I got some advice from a programmer friend that seems to be working:

The solution is to use a forward declaration.

Example:

class Station;

class Passenger{

Public:
Private:

};

This tells the Passenger class to accept that a Station class exists. I do the same thing in the Station class for passenger. Seems to have solved the issue.

Thanks again for your input!
I tried various designs to get around this issue, but wasn't able to get past the fact that I needed passengers to know what a station was, and needed stations to know what a passenger was.


Well, I don't think that is a very good design, because it would be very inefficient. If there were 100 passengers at each station and there were 50 stations, the station list of 50 would be stored for each of the 5000 passengers. This leads to m times n efficiency - which is rather bad.

There should not be a relationship between passengers and stations for the purpose of calculating a route. There would be if you wanted to calc how many passengers on a train at any given station, but that is not in the scope of the assignment, by the sounds of it.

The calculation of a route should involve the 2 stations & the Network. The Network could just be a linked list of stations for a simple example. You could use the STL <list> of station objects for this purpose. BTW, what is the complexity of the Network for the assignment? Did you mention 1 line with another branching off? If so, It would be good to see how you propose to implement your solution, with 2 classes.

I knew about the forward declaration of classes to solve circular dependencies, but didn't mention it because you needed a different design.

I look forward to your reply & hope all is well at your end.
Topic archived. No new replies allowed.