Java 类设计 - 图形

发布于 2024-09-27 16:42:10 字数 1333 浏览 5 评论 0原文

我对 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中。在我的 GraphImpl 中,因此,我的实现中有很多这样的列表。另外,您可以看到我从 addEdge 返回了一个新的 GraphImpl,因为 GraphImpl 应该是不可变的。

这样我在实现 MultiGraph 时遇到了很多麻烦,因为在这里,我需要交换 List。对于列表。但是当我在 MultiGraph 中重新定义“edges”变量时,我认为 GraphImpl 中的方法仍在访问我在 GraphImpl 中定义的列表,因此如果我调用 MultiGraph,则不会添加边,直到为 MultiGraph 完全重写它。但后来我注意到无论如何我都必须重写它,因为 GraphImpl 中的 addEdge (自然)返回一个 GraphImpl,但在 MultiGraphImpl 中,我需要创建一个 MultiGraphImpl。

我想了解的是,你将如何设计和实现这样的东西。我拥有的是一堆相互扩展的接口,以及相同的实现层次结构,也相互扩展。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把人绕傻吧 2024-10-04 16:42:10

可能您需要一个复合模式 。它允许您以类似的方式处理单个和多个对象。也许这可以帮助您的设计。

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.

ゃ人海孤独症 2024-10-04 16:42:10

这是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文