Sapphirex
Sapphirex

Reputation: 31

How to represent a Java-collection-valued attribute in UML?

Hello I'm new to UML so I got stucked.

Here's the code:

private ArrayList<Blocks> blocks = new ArrayList<Blocks>();

So I've been looking for ways to properly place that code into UML but I can't understand it. So here is some things that I'ved tried but I don't really know if correct

Here the graphic alternatives: enter image description hereimage

So which is correct and is there a better way to do it?

Upvotes: 3

Views: 936

Answers (2)

Gerd Wagner
Gerd Wagner

Reputation: 5673

UML Class Diagrams can be used both for making

  1. programming-language-agnostic design models and
  2. programming-language-specific implementation models.

In a design model, you would define blocks as a multi-valued reference property referencing a set/list of instances of a class Block (that is, Blockwould be the range of the property blocks), or, equivalently, as the name of a non-functional association end of an association between Classand Block, as in

blocks: Block[0..*] 

Such a reference property could be implemented in any OOP language according to the available idioms. E.g., in Java, it could be implemented as a Set- or List-valued property.

In an OO implementation model, like a Java Entity class model or a C# class model, you could use any suitable built-in template collection class, such as Set<Block> or List<Block>, as the range of the property blocks:

blocks: Set<Block>

Notice that normally, in a Java class model, you wouldn't use ArrayList<Block> as the range of the property blocks, but rather List<Block>, in order not to prematurely exclude the choice of implementing the property with LinkedList<Block>.

Upvotes: 2

Stephen C
Stephen C

Reputation: 718778

In one sense, anything is correct in a UML diagram provided that the people (or tools) that read the diagram understand what you mean.

In another sense, it is more conventional / appropriate to use UML syntax. UML is design language not a programming language. If you are trying to using programming language (i.e. Java) syntax for types, declarations, etc in a UML diagram you are (probably) missing the point of UML. (A UML tool is likely to object to you using Java syntax, but it depends on how closely / deeply it implements the formal UML syntax and metamodel.)

In this case, I think the most appropriate way to write that would be:

  - block : Block[]

or

  - block : Block[1..*]

The fact that you want to use ArrayList<Block> to implement the multi-value is an implementation detail. It doesn't belong in the UML diagram. (In my opinion.)

But there is a (possible) third alternative. Add Block to your UML diagram as a class, and model the relationship using an Association.

Upvotes: 3

Related Questions