PlantUML Class
PlantUML Class
PlantUML
Open-Source tool in Java to draw UML Diagram
Home
Screenshots
Download
What's New ?
Forum
Sequence Diagram
Use Case Diagram
Class Diagram
Activity Diagram
Component Diagram
State Diagram
Object Diagram
Language
Running
F.A.Q.
Current Issues
External Links
Classes
Relation between classes
Label on relations
Adding methods
Defining visibility
Notes and stereotypes
More on notes
Abstract class and interface
Using non-letters
Hide attributes, methods...
Specific spot
Packages
Namespaces
Lollipop
Arrows direction
Title
Association class
Skinparam
Skinned Stereotypes
Splitting large files
Class Diagram
To be able to generate those diagrams, you must have Graphviz software installed on your machine in the default directory c:\Program
Files\GraphvizX.XX or /usr/bin/dot.
You can have a look here if you have installed Graphviz somewhere else.
The description of class diagram is similar to the description of use case diagrams.
Extension <|--
plantuml.sourceforge.net/classes.html 1/12
1/12/2011 PlantUML
Composition *--
Agregation o--
@startuml img/classes01.png
Class01 <|-- Class02
Class03 *-- Class04
Class05 o-- Class06
Class07 .. Class08
Class09 -- Class10
Class11 <|.. Class12
Class13 --> Class14
Class15 ..> Class16
Class17 ..|> Class18
Class19 <--* Class20
@enduml
Label on relations
It is possible a add a label on the relation, using " : ", followed by the text of the label.
For cardinality, you can use double-quotes "" on each side of the relation.
@startuml img/classes02.png
@enduml
Adding methods
To declare fields and methods, you can use the symbol ":" followed by the field's or method's name.
The system checks for parenthesis to choose between methods and fields.
@startuml img/classes03.png
Object <|-- ArrayList
plantuml.sourceforge.net/classes.html 2/12
1/12/2011 PlantUML
Object : equals()
ArrayList : Object[] elementData
ArrayList : size()
@enduml
@startuml img/classes03c.png
class Dummy {
String data
void methods()
}
@enduml
Defining visibility
When you define methods or fields, you can use characters to define the visibility of the corresponding item:
@startuml img/classes03a.png
class Dummy {
-field1
#field2
~method1()
+method2()
}
@enduml
You can turn off this feature using the skinparam classAttributeIconSize 0 command :
@startuml img/classes03b.png
skinparam classAttributeIconSize 0
class Dummy {
-field1
#field2
~method1()
+method2()
}
@enduml
Stereotypes are defined with the class keyword, " << " and " >> ".
You can alse define notes using note left of , note right of , note top of , note bottom of keywords.
A note can be also define alone with the note keywords, then linked to other objects using the .. symbol.
@startuml img/classes05.png
class Object << general >>
Object <|--- ArrayList
@enduml
More on notes
It is also possible to use few html tags like :
<b>
<u>
<i>
<s>, <del> , <strike>
<font color="#AAAAAA"> or <font color="colorName">
<color:#AAAAAA> or <color:colorName>
<size:nn> to change font size
<img src="file"> or <img:file> : the file must be accessible by the filesystem
@startuml img/classes06.png
note as N1
This note is <u>also</u>
<b><color:royalBlue>on several</color>
<s>words</s> lines
And this is hosted by <img:sourceforge.jpg>
end note
plantuml.sourceforge.net/classes.html 4/12
1/12/2011 PlantUML
@enduml
@startuml img/classes04.png
enum TimeUnit
TimeUnit : DAYS
TimeUnit : HOURS
TimeUnit : MINUTES
@enduml
Using non-letters
If you want to use non-letters in the class (or enum...) display, you can either :
@startuml img/classes04a.png
class "This is my class" as class1
class class2 as "It works this way too"
plantuml.sourceforge.net/classes.html 5/12
1/12/2011 PlantUML
The basic command is: hide empty members . This command will hide attributes or methods if they are empty.
You can also provide, just after the hide or show keyword:
You can use several show/hide commands to define rules and exceptions.
@startuml img/classes11.png
class Dummy1 {
+myMethods()
}
class Dummy2 {
+hiddenMethod()
}
hide members
hide <<Serializable>> circle
show Dummy1 method
show <<Serializable>> fields
@enduml
Specific Spot
Usually, a spotted character (C, I, E or A) is used for classes, interface, enum and abstract classes.
plantuml.sourceforge.net/classes.html 6/12
1/12/2011 PlantUML
But you can define your own spot for a class when you define the stereotype, adding a single character and a color, like in this example:
@startuml img/classes08.png
Packages
You can define a package using the package keyword, and optionally declare a background color for your package (Using a html color code or name).
When you declare classes, they are automatically put in the last used package, and you can close the package definition using the end package keyword.
You can also use brackets {}.
@startuml img/classes10.png
package net.sourceforge.plantuml
Object <|-- Demo1
Demo1 *- Demo2
end package
@enduml
You can also define links between packages, like in the following example:
@startuml img/classes10a.png
package foo1.foo2
end package
package foo1.foo2.foo3 {
class Object
}
@enduml
Namespaces
In packages, the name of a class is the unique identifier of this class. It means that you cannot have two classes with the very same name in different
packages.
You can refer to classes from other namespaces by fully qualify them. Classes from the default namespace are qualified with a starting dot.
Note that you don't have to explicitly create namespace : a fully qualified class is automatically put in the right namespace.
plantuml.sourceforge.net/classes.html 7/12
1/12/2011 PlantUML
@startuml img/classes10e.png
class BaseClass
end namespace
namespace net.foo {
net.dummy.Person <|- Person
.BaseClass <|-- Person
@enduml
Lollipop interface
You can also define lollipops interface on classes, using the following syntax:
@startuml img/classes15a.png
class foo
bar ()- foo
@enduml
@startuml img/classes20a.png
Room o- Studient
Room *-- Chair
@enduml
plantuml.sourceforge.net/classes.html 8/12
1/12/2011 PlantUML
You can also change directions by reversing the link:
@startuml img/classes20b.png
Studient -o Room
Chair --* Room
@enduml
It is also possible to change arrow direction by adding left, right, up or down keywords inside the arrow:
@startuml img/classes20c.png
foo -left-> dummyLeft
foo -right-> dummyRight
foo -up-> dummyUp
foo -down-> dummyDown
@enduml
You can shorten the arrow by using only the first character of the direction (for example, -d- instead of -down-) or the two first characters (-do-).
Please note that you should not abuse this functionnality : GraphViz gives usually good results without tweaking.
You can use title and end title keywords for a longer title, as in sequence diagrams.
@startuml img/classes15.png
title Simple <b>example</b>\nof title
@enduml
Association classes
plantuml.sourceforge.net/classes.html 9/12
1/12/2011 PlantUML
You can define association class after that a relation has been defined between two classes, like in this example:
@startuml img/classes16.png
Student : Name
Student "0..*" - "1..*" Course
(Student, Course) .. Enrollment
Enrollment : drop()
Enrollment : cancel()
@enduml
@startuml img/classes17.png
Student : Name
Student "0..*" -- "1..*" Course
(Student, Course) . Enrollment
Enrollment : drop()
Enrollment : cancel()
@enduml
Skinparam
You can use the skinparam command to change colors and fonts for the drawing.
@startuml img/classes18.png
@enduml
plantuml.sourceforge.net/classes.html 10/12
1/12/2011 PlantUML
Skinned Stereotypes
You can define specific color and fonts for stereotyped classes.
@startuml img/classes18b.png
Class01 <<Foo>>
Class01 "1" *-- "many" Class02 : contains
@enduml
You can use the "page (hpages)x(vpages)" command to split the generated image into several files :
hpages is a number that indicated the number of horizontal pages, and vpages is a number that indicated the number of vertical pages.
@startuml img/classes09.png
' Split into 4 pages
page 2x2
class BaseClass
end namespace
namespace net.foo {
net.dummy.Person <|- Person
.BaseClass <|-- Person
plantuml.sourceforge.net/classes.html 11/12
1/12/2011 PlantUML
plantuml.sourceforge.net/classes.html 12/12