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

Connect with Us