Java 类设计 - 图形
我对 Java 还很陌生,我需要一些帮助来为我们分配的作业(我正在学习 CS)找出一个好的类层次结构和整体设计。
作业与图论相关,因此要求我们为两种类型的图创建接口,简单图和多重图(允许有平行边),以及相应的实现。
我想出了以下接口层次结构:
* Element - Vertex - Edge + MultiEdge * Graph - MultiGraph
及其相应的实现。 现在我真的不想在这里讨论我的实际实现,我只是给出一些例子,由于(至少我这么认为)我设计整个想法的方式而遇到了麻烦。
整个事情运行得很好,直到我需要扩展我的图形以具有多图形功能。下面是来自 GraphImpl 的代码片段:
protected final List edges; public Graph addEdge(Edge e) { List newEdges = new ArrayList<Edge>(); newEdges.addAll(edges); newEdges.add(e); return new GraphImpl(vertices, newEdges); }
如您所见,我将图边存储在 List
这样我在实现 MultiGraph 时遇到了很多麻烦,因为在这里,我需要交换 List
我想了解的是,你将如何设计和实现这样的东西。我拥有的是一堆相互扩展的接口,以及相同的实现层次结构,也相互扩展。
Graph 的功能只是 MultiGraph 的一个子集,因此 GraphImpl 中所做的一切大部分也对 MultiGraphImpl 有效。现在,我需要将大量代码从 GraphImpl 复制到 MultiGraphImpl,只是为了克服类型问题(我至少以某种方式理解,但我不知道如何规避它们)。
我希望你现在还没有太困惑,因为我确实很困惑;)如果我在任何部分不清楚,我很乐意澄清,只需指出我缺少的内容即可。
I'm fairly new to Java, and I need some help figuring out a good class hierarchy and overall design for an assignment we've been given (i'm studying CS).
The assignment is graph-theory related, so we were asked to create interfaces for 2 types of graphs, simple graphs and multi-graphs (which are allowed to have parallel edges), and the corresponding implementation.
I came up with the following interface hierarchy:
* Element - Vertex - Edge + MultiEdge * Graph - MultiGraph
And their corresponding implementations.
Now I really don't want do discuss my actual implementation here, I'm just giving some examples which I had trouble with because of (at least I think so) the way I designed the whole think.
The whole thing worked pretty well until I needed to extend my Graph to have MultiGraph functionality. Here's a code snippet from GraphImpl:
protected final List edges; public Graph addEdge(Edge e) { List newEdges = new ArrayList<Edge>(); newEdges.addAll(edges); newEdges.add(e); return new GraphImpl(vertices, newEdges); }
As you can see, I store the graphs edges in a List<Edge> in my GraphImpl, and therefor, I have lots of those lists all over my implementation. Also, you can see that I return a new GraphImpl from addEdge, as GraphImpl is supposed to be immutable.
With this I ran into a lot of trouble when implementing MultiGraph, because here, I needed to exchange the List<Edge> for a List<MultiEdge>. But when I redefined the "edges" variable in MultiGraph, I think that the methods in GraphImpl were still accessing the list I defined in GraphImpl, so that edges wouldn't get added if I called MultiGraph, until completely rewrote it for MultiGraph. But later I noticed that I would've had to rewrite it anyway, because addEdge in GraphImpl returns (naturally) a GraphImpl, but in MultiGraphImpl, I would've needed a MultiGraphImpl to be created.
What I am trying to understand is, how would you design and implement such a thing. What I have is a bunch of interfaces extending each other, and the same hierarchy of implementations, also extending each other.
The functionality of Graph is just a subset of MultiGraph, so everything that is being done in GraphImpl is mostly also valid for MultiGraphImpl. Right now, I needed to copy lots of code from GraphImpl to MultiGraphImpl, just to overcome type problems (which I, at least somehow, understand, but I wouldn't know how to circumvent them).
I hope you're not too confused by now, because I definitely am ;) If I was unclear in any part, I'll be happy to clarify, just point me towards whats missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里有一些注释非常好的实现图论对象和算法的 Java 源代码,希望与您的问题相关。 (您可能想要从这里开始阅读。)
祝你好运!
Here is some very well-annotated Java source code implementing graph-theoretical objects and algorithms, hopefully relevant to your problem. (You probably want to start reading here.)
Good luck!
可能您需要一个复合模式 。它允许您以类似的方式处理单个和多个对象。也许这可以帮助您的设计。
Could be that you need a Composite Pattern here. It lets you treat single and multi objects in a similar way. Perhaps that can help your design.
这是 Goodrich/Tamassia 为《Java 中的数据结构和算法》一书设计的非常完整的数据结构。
尝试一下: http://net3.datastructs.net/
而且,是的,我确实支持多-图形
The is a very complete datastructures designed by Goodrich/Tamassia for the book Data Structures and Algorithms in Java.
Give it a try it: http://net3.datastructures.net/
And, yes, i does support multi-graph