I'm designing an application whereby there are links and nodes. Each of the two ends of a link is connected to one node and each node can be connected to multiple links.
There are naturally two ways to design the Link and Node classes. The first: Links own Nodes. Nodes will have no knowledge of links.
class Link
{
public Node First {get; set;}
public Node Second {get; set;}
public string Name {get; set;}
public double Length {get; set;}
public double Strength {get; set;}
}
class Node
{
// ...
}
The second: Node owns Links. Links will have no knowledge of nods.
class Link
{
public string Name {get; set;}
public double Length {get; set;}
public double Strength {get; set;}
}
class Node
{
public IList<Link> Links {get; set;}
//...
}
I am not in favor of the second design as it implies the possibility of more than two nodes being connected to the same single link. Furthermore it does not allow a node instance to find its linked partner. If that is the case, unlinking it will be hard (imagine setting the Link
property of a node to null - its partner will still hold a reference to the Link
)
On the other hand, the first design would imply a need to keep track of the Link
instances. Deleting for example a node that is linked would cause a need to explicitly delete the corresponding Link
instance (unless we allow the reference to the node in the link to be null - but I want to avoid this as well).
Which do you think, or rather, which is more correct from an OOP point of view?