0% found this document useful (0 votes)
193 views12 pages

PlantUML Class

The document describes how to draw class diagrams using PlantUML, an open-source tool for generating UML diagrams. It explains how to define classes, relationships between classes, methods, fields and more.

Uploaded by

Peter Pham
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
193 views12 pages

PlantUML Class

The document describes how to draw class diagrams using PlantUML, an open-source tool for generating UML diagrams. It explains how to define classes, relationships between classes, methods, fields and more.

Uploaded by

Peter Pham
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1/12/2011 PlantUML

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.

Relations between classes


Relations between classes are defined using the following symbols :

Extension <|--

plantuml.sourceforge.net/classes.html 1/12
1/12/2011 PlantUML

Composition *--
Agregation o--

It is possible to replace -- by .. to have a dotted line.

Knowing thoses rules, it is possible to draw the following drawings:

@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

Class01 "1" *-- "many" Class02 : contains

Class03 o-- Class04 : agregation

Class05 --> "1" Class06

@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

It is also possible to group between brackets {} all fields and methods.

@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:

Character Icon for field Icon for method Visibility


- private
# protected
~ package private
+ public

@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

Notes and stereotypes


plantuml.sourceforge.net/classes.html 3/12
1/12/2011 PlantUML

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

note top of Object : In java, every class\nextends this one.

note "This is a floating note" as N1


note "This note is connected\nto several objects." as N2
Object .. N2
N2 .. 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

You can also have a note on several lines

@startuml img/classes06.png

note top of Object


In java, <size:18>every</size> <u>class</u>
<b>extends</b>
<i>this</i> one.
end note

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

Abstract class and interface


You can declare a class as abstract using "abstract" or "abstract class" keywords.

The class will be printed in italic.

You can use the interface and enum keywords too.

@startuml img/classes04.png

abstract class AbstractList


abstract AbstractCollection
interface List
interface Collection

List <|-- AbstractList


Collection <|-- AbstractCollection

Collection <|- List


AbstractCollection <|- AbstractList
AbstractList <|-- ArrayList

ArrayList : Object[] elementData


ArrayList : size()

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 :

Use the as keyword in the class definition


Put quotes "" around the class name

@startuml img/classes04a.png
class "This is my class" as class1
class class2 as "It works this way too"

class2 *-- "foo/dummy" : use


@enduml

plantuml.sourceforge.net/classes.html 5/12
1/12/2011 PlantUML

Hide attributes, methods...


You can parameterize the display of classes using the hide/show command.

The basic command is: hide empty members . This command will hide attributes or methods if they are empty.

Instead of empty members , you can use:

empty fields or empty attributes for empty fields,


empty methods for empty methods,
fields or attributes which will hide fields, even if they are described
methods wich will hide methods, even if they are described
members wich will hide fields and methods, even if they are described
circle for the circled character in front of class name,
stereotype for the stereotype.

You can also provide, just after the hide or show keyword:

class for all classes,


interface for all interfaces,
enum for all enums,
<<foo1>> for classes which are stereotyped with foo1,
an existing class name.

You can use several show/hide commands to define rules and exceptions.

@startuml img/classes11.png

class Dummy1 {
+myMethods()
}

class Dummy2 {
+hiddenMethod()
}

class Dummy3 <<Serializable>> {


String name
}

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

class System << (S,#FF7700) Singleton >>


class Date << (D,orchid) >>
@enduml

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 {}.

Note that package definitions can be nested.

@startuml img/classes10.png

package "Classic Collections" #DDDDDD {


Object <|-- ArrayList
}

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
}

foo1.foo2 +-- foo1.foo2.foo3

@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.

In that case, you should use namespaces instead of 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

namespace net.dummy #DDDDDD


.BaseClass <|-- Person
Meeting o-- Person

.BaseClass <|- Meeting

end namespace

namespace net.foo {
net.dummy.Person <|- Person
.BaseClass <|-- Person

net.dummy.Meeting o-- Person


}

BaseClass <|-- net.unused.Person

@enduml

Lollipop interface
You can also define lollipops interface on classes, using the following syntax:

bar ()- foo


bar ()-- foo
foo -() bar

@startuml img/classes15a.png
class foo
bar ()- foo
@enduml

Changing arrows direction


By default, links between classes have two dashes -- and are verticaly oriented. It is possible to use horizontal link by putting a single dash (or dot) like this:

@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.

Title the diagram


The title keywords is used to put a title.

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

Object <|-- ArrayList

@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

You can define it in another direction:

@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.

You can use this command :

In the diagram definition, like any other commands,


In an included file,
In a configuration file, provided in the command line or the ANT task.

@startuml img/classes18.png

skinparam classBackgroundColor PaleGreen


skinparam classArrowColor SeaGreen
skinparam classBorderColor SpringGreen
skinparam stereotypeCBackgroundColor YellowGreen

Class01 "1" *-- "many" Class02 : contains

Class03 o-- Class04 : agregation

@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

skinparam classBackgroundColor PaleGreen


skinparam classArrowColor SeaGreen
skinparam classBorderColor SpringGreen
skinparam stereotypeCBackgroundColor YellowGreen
skinparam classBackgroundColor<<Foo>> Wheat
skinparam classBorderColor<<Foo>> Tomato
skinparam stereotypeCBackgroundColor<<Foo>> DimGray

Class01 <<Foo>>
Class01 "1" *-- "many" Class02 : contains

Class03 <<Foo>> o-- Class04 : agregation

@enduml

Splitting large files


Sometimes, you will get some very large image files.

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

namespace net.dummy #DDDDDD


.BaseClass <|-- Person
Meeting o-- Person

.BaseClass <|- Meeting

end namespace

namespace net.foo {
net.dummy.Person <|- Person
.BaseClass <|-- Person

net.dummy.Meeting o-- Person


}

BaseClass <|-- net.unused.Person


@enduml

plantuml.sourceforge.net/classes.html 11/12
1/12/2011 PlantUML

Back to top Contact

plantuml.sourceforge.net/classes.html 12/12

You might also like