Reputation: 33223
I am trying to figure out how I should arrange my classes so that I can achieve a following.
So, ultimately I have a graph which takes nodes and edges.
And the idea is to run some graph algorithm based on these nodes and edges.
So, the basic unit Graph class takes are
private Node node
private Edge Edge
and you form a graph .. right
Now, here is the issue.
I have a special Edge say special Edge which implements the earlier Edge and has some more special properties of its own.
So I have a graph of type Node, SpecialEdge
Now, how do I use graph G which accepts Edge and not SpecialEdge??
Any suggestions would be appreciated. Thanks
Upvotes: 1
Views: 126
Reputation: 36
You can define an interface Edge where subclasses like SpecialEdge would implement it. You may have many implementations of Edge and it will always work.
Also, since your Graph contains many nodes and many edges, you may consider using List<Node>
and List<Edge>
in your design.
Edit: According to Nick, you should use Set<Node>
and Set<Edge>
since the order is not an issue in this case.
Upvotes: 2
Reputation: 41281
You should create two different subclasses of Edge, namely BasicEdge and SpecialEdge.
A graph that should accept both should be Edge. If you only want one type, accept either BasicEdge or SpecialEdge.
Upvotes: 2