Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Railway station object design



Permlink Replies: 2 - Last Post: Oct 28, 2015 10:17 PM Last Post By: Derek Seymour
Derek Seymour

Posts: 11
Registered: 10/8/15
Railway station object design
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 20, 2015 10:03 PM
Suppose I need to model a railway. What would an appropriate design be?

At the moment, I have a object called Railway controller, which has a a list of objects for stations, and a list of objects for lines. At the moment, the Station objects don't know anything about the line objects, but the lines have a reference to the start station and end station.. Everything is done via the controller. So when you add a station the controller does it. When you add a line the controller adds a line which has a reference to the station start, and station end.

This is now working well. However, my current problem is that I need something (I'm guessing a tree) so that when a station is moved to different place, all the lines will move and all the stations associated with that line will also move. My design can't accomodate this at present (since no station knows about its line), I don't think. At best I can look at each line and see where it starts and ends, but I can't know where the next station attached to the end station is going.

Hope that's clear? ;-)

Anyhow, seems like it's time to add a tree of some kind, but not sure where to add it, in order to achieve a good OO design. Thoughts (if you got this far, you legends!)

Edited by: Derek Seymour on Oct 20, 2015 10:03 PM
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: Railway station object design [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 21, 2015 10:59 AM   in response to: Derek Seymour in response to: Derek Seymour
Derek Seymour wrote:

Suppose I need to model a railway. What would an appropriate design
be?

At the moment, I have a object called Railway controller, which has
a a list of objects for stations, and a list of objects for lines.

This looks too simplistic from the start. Of course that depends on
what you call a "line" and a "station", but in a real railway system
each station (possible stop for a train) can sit on a single track or
an arbitrarily complex intersection of more than one track. The trains
rolling on the tracks connect a start station with an end station, but
in between you can have any number of intermediate stations the train
either stops at or passes through.

The stations and tracks form a complex network (a graph with nodes
connected by lines), with no fixed limit on the number of connections a
node can have. The connections can be uni- or bidirectional. Each node
would have a collection of lines, and each line would have a start and
end node. If a track can branch between nodes (so you can reach two end
stations from one start station) the branch point is just treated as a
node itself, just one the train cannot stop at. This way you can rely
on the basic assumption that each connection will connect exactly two
nodes.

The representation in a program design is indeed a kind of tree, but
one that has no real root (any node can serve as the root), and each
node can have an arbitrary number of connections. If the connections do
not need to have properties of their own (directionality, length,
restrictions on the kind of traffic they can carry, maximum speed
allowed, times of day no traffic is allowed, and so on) you would not
actually need a representation of the connection as a separate class,
the connection would be implicit in the collection of connected
neighbor nodes each node has. Otherwise you would have a connection
class, and each node would have a collection of connections that are
available from it. Each connection would have references to the two
nodes it connects.

Your railway manager would have two collections, one for nodes and one
for connections. The fun starts when you need to find a path through
the network that connects two distant nodes while fullfilling a set of
criteria (traveling salesman problem, NP complete). Adding and removing
nodes or rearranging connections is also a problem.

In addition to the network of stations and connections you would
probably also have to model what I would understand as "line" in this
context: a train running a particular sequence of stations. The class
representing it would have a description of this sequence of some kind.
You can model that as a collection of nodes, or of connections together
with the start node, both can be traversed to reconstruct the path
taken by the train. The condition is, of course, that each node has a
direct connection to the following one.

--
Peter Below
TeamB

Derek Seymour

Posts: 11
Registered: 10/8/15
Re: Railway station object design [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 28, 2015 10:17 PM   in response to: Peter Below in response to: Peter Below
Peter Below wrote:
Derek Seymour wrote:

Suppose I need to model a railway. What would an appropriate design
be?

At the moment, I have a object called Railway controller, which has
a a list of objects for stations, and a list of objects for lines.

This looks too simplistic from the start. Of course that depends on
what you call a "line" and a "station", but in a real railway system
each station (possible stop for a train) can sit on a single track or
an arbitrarily complex intersection of more than one track. The trains
rolling on the tracks connect a start station with an end station, but
in between you can have any number of intermediate stations the train
either stops at or passes through.

The stations and tracks form a complex network (a graph with nodes
connected by lines), with no fixed limit on the number of connections a
node can have. The connections can be uni- or bidirectional. Each node
would have a collection of lines, and each line would have a start and
end node. If a track can branch between nodes (so you can reach two end
stations from one start station) the branch point is just treated as a
node itself, just one the train cannot stop at. This way you can rely
on the basic assumption that each connection will connect exactly two
nodes.

The representation in a program design is indeed a kind of tree, but
one that has no real root (any node can serve as the root), and each
node can have an arbitrary number of connections. If the connections do
not need to have properties of their own (directionality, length,
restrictions on the kind of traffic they can carry, maximum speed
allowed, times of day no traffic is allowed, and so on) you would not
actually need a representation of the connection as a separate class,
the connection would be implicit in the collection of connected
neighbor nodes each node has. Otherwise you would have a connection
class, and each node would have a collection of connections that are
available from it. Each connection would have references to the two
nodes it connects.

Your railway manager would have two collections, one for nodes and one
for connections. The fun starts when you need to find a path through
the network that connects two distant nodes while fullfilling a set of
criteria (traveling salesman problem, NP complete). Adding and removing
nodes or rearranging connections is also a problem.

In addition to the network of stations and connections you would
probably also have to model what I would understand as "line" in this
context: a train running a particular sequence of stations. The class
representing it would have a description of this sequence of some kind.
You can model that as a collection of nodes, or of connections together
with the start node, both can be traversed to reconstruct the path
taken by the train. The condition is, of course, that each node has a
direct connection to the following one.

--
Peter Below
TeamB

Thanks Peter, what I've got so far is very similar. I have nodes, which are essentially trees (each node has a tree), and connections (start, end) which are used to provide visuals between them. Visually it seems to be working. Not much logic attached though. I don't traverse my tree via the lines - this is done at the moment via the nodes.

It's getting a wee bit complex.

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02