UML Class Diagram and Generics
Asked Answered
R

7

45

How do you indicate a generic class or interface in a Class Diagram?

I'm looking for two things really:

  1. One is how do I model generic java classes if i were doing it with pen and paper.
  2. Secondly, what uml tools out there support generic classes/interfaces.

Thanks!

Reflexion answered 24/1, 2011 at 20:25 Comment(0)
M
40

Usually generics are represented as classifier's template parameter. Here is the example how it looks like in the NetBeans UML Plugin:

enter image description here

Martella answered 24/1, 2011 at 20:46 Comment(3)
Where can you get the Netbeans UML plugin?Reflexion
@Karthik you can get it here netbeans.org/downloads/zip.html, see netbeans-6.9.1-201007282301-ml-uml.zipMartella
Using the implements arrow to indicate generic type construction seems problematic, as it carries some different implications compared to interface implementation. Notably, if the implements arrow of multiple classes meet at the same interface, that's a visual clue that we could e.g. declare a single list of that type that can hold instances of all the implementing classes. The same cannot be said if the implements arrows denote generic type construction.Pomology
H
20

UML calls them parameterized types - see examples here.

As for tools, I know MagicDraw supports them.

Him answered 24/1, 2011 at 20:40 Comment(0)
C
5

UML template will help !

Defining Generics with UML Templates
http://www.eclipse.org/articles/article.php?file=Article-Defining-Generics-with-UML-Templates/index.html

Capper answered 24/1, 2011 at 20:36 Comment(0)
K
4

I'm looking for two things really: One is how do I model generic java classes if i were doing it with pen and paper.

Craig Larman's book Applying UML and Patterns 3e has instructor slides with the Figures from the book. The following is from Figure 15.5:

enter image description here

Secondly, what uml tools out there support generic classes/interfaces.

I like PlantUML, which will produce the following image from

skinparam style strictuml
participant sales as "sales:\nArrayList<Sale>"

enter image description here

For class diagrams, it produces

skinparam style strictuml
hide empty members
class "ArrayList<Sale>" as S
class Store {
Store()
}
Store "1" -> "1" S : "            sales  "

enter image description here

Kimon answered 19/6, 2017 at 0:34 Comment(1)
FFWD 2019 to here plantuml.com/class-diagram -- plantUML now does "proper" genericsExegete
R
3

One is how do I model generic java classes if i were doing it with pen and paper.

AFAIK there is no specific support for this in UML, the best may be to use a stereotype like <<generic>>. But on paper with pen, there is nothing simpler than writing MYType<T> :-)

Secondly, what uml tools out there support generic classes/interfaces.

Stereotypes are supported by all UML tools I have seen so far. Of course, the level of support may differ; however, since I hardly use any UML tools apart from napkins :-) I don't have much base for comparison here.

Rech answered 24/1, 2011 at 20:32 Comment(1)
pls see other answers. UML does have specific support for generics without having to resort to stereotypes. hth.Him
R
0

In Netbeans The object to use is called "Derivation Classifier". This is the object to use for MyGeneric in Answer from Vitalii Fedorenko

Ryan answered 8/11, 2013 at 12:36 Comment(0)
C
0

One is how do I model generic java classes if i were doing it with pen and paper.

It is generalization arrow, base class, if it is abstract, should be written in italics. Also can be marked with stereotype.

Secondly, what uml tools out there support generic classes/interfaces.

Use http://staruml.io/. Really nice tool, you can manage all project diagrams in one place. At right top corner, there is object/diagram tree, so you can use same classes(other objects) at different diagrams.

Cultured answered 12/7, 2017 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.