简体   繁体   中英

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

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<T> implements Iterable<I> class (with private Node<T> head ). This class also has a static inner class Node<D> .

The GenericLinkedList<T> implements the abstract method iterator() (from Iterable<I> ) that returns an Iterator<T> . 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.

  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? 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<D> and GLLIterator<T> 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.

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<T> and GLLIterator<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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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