简体   繁体   English

Java 和 UML:LinkedList 类和迭代器接口之间的关系

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

I have a few questions on UML relationships in Java.我有几个关于 Java 中 UML 关系的问题。

The following classes and interfaces refer to classes and interfaces (either created or existing) in Java.以下类和接口指的是 Java 中的类和接口(创建的或现有的)。

Setup:设置:

Suppose I have a GenericLinkedList<T> implements Iterable<I> class (with private Node<T> head ).假设我有一个GenericLinkedList<T> implements Iterable<I>类(带有私有Node<T> head )。 This class also has a static inner class Node<D> .这个类还有一个静态内部类Node<D>

The GenericLinkedList<T> implements the abstract method iterator() (from Iterable<I> ) that returns an Iterator<T> . GenericLinkedList<T>实现了返回Iterator<T>的抽象方法iterator() (来自Iterable<I> )。 The class that implements Iterator<T> , GLLIterator<T> , takes an GenericLinkedList<T> object as a parameter (in its constructor), but just stores the head of this list in a private cur variable.实现Iterator<T>GLLIterator<T>GenericLinkedList<T>对象作为参数(在其构造函数中),但只是将这个列表的头部存储在一个私有的cur变量中。

  1. Would the relationship between GLLIterator<T> and GenericLinkedList<T> be Composition (with the black diamond pointing to the list), even though the latter does not contain the former? GLLIterator<T>GenericLinkedList<T>之间的关系GLLIterator<T>是 Composition (黑色菱形指向列表),即使后者不包含前者? 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 ?或者它只是一个依赖项,因为迭代器只是“使用”列表来获取head
  2. Would the relationship between Node<D> and GLLIterator<T> be Aggregation or Composition? Node<D>GLLIterator<T>之间的关系是聚合还是组合? 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.但是,另一方面,当cur节点死亡时,迭代器就没有意义了。
  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.编辑:我试图通过(1)推理如下:所以,链表有节点,迭代器有一个节点。 If Java had destructors, would I need to call the destructor of an iterator if I destroy the linked list?如果 Java 有析构函数,如果我销毁链表,是否需要调用迭代器的析构函数? 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.但是迭代器存在于列表之外,它(1)只指向列表中的一个节点,(2)可以用作另一个列表的迭代器。 I ask because I was wondering the UML relationship between the iterator classes and the linked list class: Composition (owns) or Aggregation (has-a).我问是因为我想知道迭代器类和链表类之间的 UML 关系:组合(拥有)或聚合(has-a)。

Thank you.谢谢你。

If Java had destructors, would I need to call the destructor of an iterator if I destroy the linked list?如果 Java 有析构函数,如果我销毁链表,是否需要调用迭代器的析构函数?

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<T> and GLLIterator<T> .对我来说,列表本身不需要知道迭代器,迭代器本身也不需要知道列表,所以对于问题 1 GenericLinkedList<T>GLLIterator<T>之间GenericLinkedList<T>没有关系。

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.对于问题2没有聚合也没有组合,因为迭代器只是引用了一个节点, the iterator has a node是假的,一个迭代器不是由节点组成也不拥有它们。 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.在迭代器中,您与节点有一个简单的关联,多重性可以是0..1 (0 表示迭代器无效)或1具体取决于实现。

For the question 3 an interface having no implementation it cannot use something else, contrarily to the implementing class(es).对于问题 3,一个没有实现的接口不能使用其他东西,与实现类相反。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM