1

I'm divided as to where to place a method which checks whether two edges are parallel in a graph.

In the UML class diagram seen below, I have placed my suggestions in notes.

enter image description here

The figure depicts that a Graph consits of Edges and Vertices. Two edges are parallel if they associate with the same vertices (both of the edges and all of the vertices mustu belong to the same graph).

My suggestions are (horizontally ordered):

  • A graph can answer whether two edges (which it must contain) are parallel.
  • An Edge classifier can answer whether two edges are parallel.
  • An Edge can inspect another edge to answer whether it is parallel to itself.

My question is: What is the argument for and against each of my three proposals?

Pétur Ingi Egilsson
  • 4,368
  • 5
  • 44
  • 72
  • 2
    I wouldn't make it a method of the Graph (it's not a property of the graph). I would prefer the isParallelTo, since you have to have instances of Edge anyway and there seems to be no need for a static method (it's quite similare to equals() or compareTo()) – Nitek Jun 03 '15 at 05:53
  • Can you make this an answer, I will accept it if the answer to http://math.stackexchange.com/questions/1310259/are-two-edges-parallel-even-when-they-do-not-belong-to-the-same-graph is yes, as your answer depends on an edge not being bound to a specific graph. – Pétur Ingi Egilsson Jun 03 '15 at 06:22

3 Answers3

2

I wouldn't make it a method of the Graph (it's not a property of the graph). I would prefer the isParallelTo, since you have to have instances of Edge anyway and there seems to be no need for a static method (it's quite similar to the equals() or compareTo() methods).

In case the same edges might not be parallel depending on the graph they are in, of course it would make sense to add the check to the Graph object, because it is the one who can decide whether or not the given edges are parallel.

Nitek
  • 2,515
  • 2
  • 23
  • 39
1

I would definitely go for isParallelTo(Edge b)

Possible reasons:

  • Since apparently the only things needed to decide whether they are parallel is the Edge and its Vertices, and you don't need anything from Graph, I don't think it belongs on the Graph. There is nothing Graph knows that is required to determine parallelism.

  • Static methods cannot be overridden. That would be a problem if you had subclasses of Edge that needed to override isParallelTo()

Geert Bellekens
  • 12,788
  • 2
  • 23
  • 50
0

I would use two nonstatic methods in Edge. One method to ask if another Edge is parallel, then another method to ask the other Edge if it connects to a Vertex. Both of those methods can be overridden in a subclass, and the latter prevents an Edge from rifling through another Edge's Vertexes to determine if it is parallel.

Jim L.
  • 6,177
  • 3
  • 21
  • 47