2

I am trying to design my classes in the optimal way for the following scenario. A city has metro lines covering a number of stations.There are 4 lines represented by 4 colors.The user has to enter start and end stations and we should return the shortest route to take and cost of travel.The cost is constant from one station to adjacent station- $2.If they need to switch lanes then additional $1 needs to be paid. enter image description here

Here are the classes and methods I have designed for this.

enter image description here

Is this a good design? What improvements can be made to this for it to be reusable and robust?What object oriented principles can be applied to make this design better?

here is link to my class diagram, if the above one is not clear https://creately.com/diagram/i1ay21xi2/55WPjrH3iHFsDyi8Bs6sZgTDksw%3D

5
  • 1
    Some stations serve more than one line, so getLaneColor may not be appropriate, you may want to model the intersection points and assign that to your station object. For some stations intersections will be null or empty.
    – Jon Raynor
    Commented Oct 15, 2014 at 18:38
  • 4
    Voting to close as too broad. Design review questions are on-topic for the site, but the review needs to be scoped. Merely asking about reusability, robustness, or following OOP are not sufficient to scope the request. I'll retract my close vote (or vote to re-open) if the question is edited and the scope decreased.
    – user53019
    Commented Oct 16, 2014 at 1:09
  • You could also do an object-oriented analysis of the problem (a domain model) before you start coding it up. Commented Oct 16, 2014 at 13:45
  • Also, this sounds like it could be homework. If it is, you should say so. meta.stackexchange.com/questions/10811/… Commented Oct 16, 2014 at 13:49
  • Do a little reading up on "Graph Theory" and how to do various types of graph walking and you will find this problem has been solved many-many times already.
    – Dunk
    Commented Oct 16, 2014 at 15:46

1 Answer 1

6

Basically a Subway network is a collection of line, a line is a collection of stations and a station a collection of intersections (1 or more other stations connected) .

You could have your Subway network represented by a basic hashMap with all your lines.

Your stations could be modeled as a collection of intersection/sequences

Subway network
 - Lines (hashMap could do it)

Line
 - Stations (hashMap could do it)

Station
 - Intersections (hashMap could do it)

Intersection (structured as a 2 sided linked list)
 - line
 - previous station
 - next station

Considering your object model as you presented it, you should avoid using profusely get/set or you will end un with an anemic model.

Your methods should look like:

  • Line::calculateTravelPrice(Station1, Station2)
  • Line::intersects(line)
  • Station::isEndOfLine()

Basically you let each component do to what it's made for.

You will have to think more about your specific domain and what you need to be able to do, but this answer might be a nice beginning

2
  • +1 But I'm no sure whether or not every station is an intersection even when the line doesn't intersect any other line in that station. I think an interception needs at least two lines to overlap in one station. Commented Oct 15, 2014 at 19:33
  • I came up with intersection as a connecting segment, it could be used to represent the end of the line (previous station only), or an element in the middle of the line (one previous station and one next station). In the end the station is a collection of all these connections
    – JF Dion
    Commented Oct 15, 2014 at 19:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.