Java和UML:LinkedList类与Iterator接口之间的关系

huangapple go评论67阅读模式
英文:

Java and UML: Relationship between a LinkedList class and an Iterator Interface

问题

以下是翻译好的内容:

我对Java中的UML关系有一些问题。

以下的类和接口是与Java中的类和接口(已创建或已存在)相关的。

设置:

假设我有一个GenericLinkedList<T> implements Iterable<I>类(具有私有的Node<T> head)。这个类还有一个静态内部类Node<D>

GenericLinkedList<T>实现了抽象方法iterator()(来自Iterable<I>),该方法返回一个Iterator<T>
实现了Iterator<T>的类GLLIterator<T>以一个GenericLinkedList<T>对象作为参数(在其构造函数中),但只是将该列表的头存储在一个私有的cur变量中。

  1. 即使后者不包含前者,GLLIterator<T>GenericLinkedList<T>之间的关系是否是组合(用黑色菱形指向列表)?我认为是,因为没有“列表”,“迭代器”就没有意义。还是说它只是一种依赖关系,因为迭代器只是使用列表来获取head
  2. Node<D>GLLIterator<T>之间的关系是聚合还是组合?我认为是聚合,因为我们可以在同一个节点上有多个迭代器。但另一方面,当cur节点失效时,迭代器就没有意义了。
  3. 在列表类和迭代器接口之间,或者在实现该接口的类之间,我应该绘制一个依赖关系?

编辑:我试图通过以下方式推理(1):
所以,链表有节点,迭代器也有一个节点。
如果Java有析构函数,如果我销毁链表,是否需要调用迭代器的析构函数?
如果我销毁链表,节点的析构函数应该会被调用。
但是迭代器存在于列表之外,它(1)只是指向列表中的一个节点,并且(2)可以用作另一个列表的迭代器。
我问这个问题是因为我想知道迭代器类与链表类之间的UML关系:组合(拥有)还是聚合(包含)。

谢谢。

英文:

I have a few questions on UML relationships in Java.

The following classes and interfaces refer to classes and interfaces (either created or existing) in Java.

Setup:

Suppose I have a GenericLinkedList&lt;T&gt; implements Iterable&lt;I&gt; class (with private Node&lt;T&gt; head). This class also has a static inner class Node&lt;D&gt;.

The GenericLinkedList&lt;T&gt; implements the abstract method iterator() (from Iterable&lt;I&gt;) that returns an Iterator&lt;T&gt;.
The class that implements Iterator&lt;T&gt;, GLLIterator&lt;T&gt;, takes an GenericLinkedList&lt;T&gt; object as a parameter (in its constructor), but just stores the head of this list in a private cur variable.

  1. Would the relationship between GLLIterator&lt;T&gt; and GenericLinkedList&lt;T&gt; be Composition (with the black diamond pointing to the list), even though the latter does not contain the former? I thought it would because the "iterators" are meaningless without the the "lists". Or would it just be a dependency, since the iterator just "uses" the list to get head?
  2. Would the relationship between Node&lt;D&gt; and GLLIterator&lt;T&gt; be Aggregation or Composition? I thought it would be Aggregation because we can have multiple iterators at the same node. But, on the other hand, when the cur node dies, the iterator is meaningless.
  3. Would I draw a dependency between the list class and the iterator interface or the class that implements the interface?

Edit: I was trying to reason through (1) as follows:
So, the linked list has nodes and the iterator has a node.
If Java had destructors, would I need to call the destructor of an iterator if I destroy the linked list?
If I destroy the linked list, the destructors of the nodes should get called.
But the iterator exists apart from the list, it (1) just points to a node in the list and (2) can be used as an iterator for another list.
I ask because I was wondering the UML relationship between the iterator classes and the linked list class: Composition (owns) or Aggregation (has-a).

Thank you.

答案1

得分: 1

如果Java拥有析构函数,那么如果我销毁了链表,是否需要调用迭代器的析构函数?

删除迭代器,因为它对应于已删除的节点(无论列表是否已删除,因此所有节点是否已删除),对我来说是最糟糕的选择,这意味着迭代器变得静默不可用,这是引入执行中未定义行为的非常实用的方式。

如果节点知道其迭代器,一种好的方法是在删除节点时将它们标记为无效,在这种情况下,尝试访问列表的相应元素或转到前一个/后一个元素会产生异常。

对我来说,列表本身不需要知道迭代器,迭代器本身也不需要知道列表,因此对于问题1,GenericLinkedList&lt;T&gt;GLLIterator&lt;T&gt;之间没有任何关系。

对于问题2,没有聚合也没有组合,因为迭代器只引用一个节点,迭代器拥有一个节点是错误的,迭代器不由节点组成,也不拥有它们。反过来,即使一个节点知道指向它的迭代器,该节点也不由迭代器组成,也不拥有它们,只是简单地引用它们。
如果一个节点知道迭代器,则从节点到迭代器的关联具有多重性 *,否则根本没有关系。
在迭代器中,您与节点有一个简单的关联,多重性可以是 0..1(0 表示迭代器无效)或 1,具体取决于实现方式。

对于问题3,一个没有实现的接口不能使用其他东西,与实现类不同。

英文:

> If Java had destructors, would I need to call the destructor of an iterator if I destroy the linked list?

To delete an iterator because it corresponds to a deleted node (whatever the list is deleted or not, so all its nodes are deleted or not) is for me the worst possible choice, that means the iterator becomes silently unusable, a very practical way to introduce undefined behaviors at the execution.

In case a node knows its iterators a good way is to mark them invalid when the node is deleted, and in that case to try access the corresponding element of the list or go to the previous/next element produces an exception.

For me the list by itself do not need to know the iterators, and an iterator does not need to know the list by itself, so for question 1 no relation at all between GenericLinkedList&lt;T&gt; and GLLIterator&lt;T&gt;.

For the question 2 no aggregation nor composition, because the iterator just references a node, the iterator has a node is false, an iterator is not composed of nodes nor owns them. In the reverse direction even a node knows the iterators pointing to it that node is not composed of iterator nor owns them, but also just reference them.
If a node knows the iterators you have an association from node to iterator with the multiplicity *, else no relation at all.
In iterator you have a simple association to node, the multiplicity can be 0..1 (0 means the iterator was invalidated) or 1 depending on the implementation.

For the question 3 an interface having no implementation it cannot use something else, contrarily to the implementing class(es).

huangapple
  • 本文由 发表于 2020年10月5日 07:35:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64200864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定