12
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 <|-- 1/12/2011 PlantUML plantuml.sourceforge.net/classes.html 1/12

PlantUML_Class

Embed Size (px)

Citation preview

Page 1: PlantUML_Class

PlantUMLOpen-Source tool in Java to draw UML Diagram

HomeScreenshotsDownloadWhat's New ?ForumSequence DiagramUse Case DiagramClass DiagramActivity DiagramComponent DiagramState DiagramObject DiagramLanguageRunningF.A.Q.Current IssuesExternal Links

ClassesRelation between classesLabel on relationsAdding methodsDefining visibilityNotes and stereotypesMore on notesAbstract class and interfaceUsing non-lettersHide attributes, methods...Specific spotPackagesNamespacesLollipopArrows directionTitleAssociation classSkinparamSkinned StereotypesSplitting large files

Class DiagramTo be able to generate those diagrams, you must have Graphviz software installed on your machine in the default directory c:\ProgramFiles\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 classesRelations between classes are defined using the following symbols :

Extension <|--

1/12/2011 PlantUML

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

Page 2: PlantUML_Class

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.pngClass01 <|-- Class02Class03 *-- Class04Class05 o-- Class06Class07 .. Class08Class09 -- Class10Class11 <|.. Class12Class13 --> Class14Class15 ..> Class16Class17 ..|> Class18Class19 <--* Class20@enduml

Label on relationsIt 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 methodsTo 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.pngObject <|-- ArrayList

1/12/2011 PlantUML

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

Page 3: PlantUML_Class

Object : equals()ArrayList : Object[] elementDataArrayList : size()

@enduml

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

@startuml img/classes03c.pngclass Dummy { String data void methods()}@enduml

Defining visibilityWhen 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.pngskinparam classAttributeIconSize 0class Dummy { -field1 #field2 ~method1() +method2()}

@enduml

Notes and stereotypes

1/12/2011 PlantUML

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

Page 4: PlantUML_Class

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.pngclass Object << general >>Object <|--- ArrayList

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

note "This is a floating note" as N1note "This note is connected\nto several objects." as N2Object .. N2N2 .. ArrayList

@enduml

More on notesIt 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

1/12/2011 PlantUML

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

Page 5: PlantUML_Class

@enduml

Abstract class and interfaceYou 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 AbstractListabstract AbstractCollectioninterface Listinterface Collection

List <|-- AbstractListCollection <|-- AbstractCollection

Collection <|- ListAbstractCollection <|- AbstractListAbstractList <|-- ArrayList

ArrayList : Object[] elementDataArrayList : size()

enum TimeUnitTimeUnit : DAYSTimeUnit : HOURSTimeUnit : MINUTES

@enduml

Using non-lettersIf you want to use non-letters in the class (or enum...) display, you can either :

Use the as keyword in the class definitionPut quotes "" around the class name

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

class2 *-- "foo/dummy" : use@enduml

1/12/2011 PlantUML

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

Page 6: PlantUML_Class

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 describedmethods wich will hide methods, even if they are describedmembers wich will hide fields and methods, even if they are describedcircle 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 membershide <<Serializable>> circleshow Dummy1 methodshow <<Serializable>> fields

@enduml

Specific SpotUsually, a spotted character (C, I, E or A) is used for classes, interface, enum and abstract classes.

1/12/2011 PlantUML

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

Page 7: PlantUML_Class

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

PackagesYou 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 *- Demo2end package

@enduml

You can also define links between packages, like in the following example:

@startuml img/classes10a.png

package foo1.foo2end package

package foo1.foo2.foo3 { class Object}

foo1.foo2 +-- foo1.foo2.foo3

@enduml

NamespacesIn 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 differentpackages.

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.

1/12/2011 PlantUML

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

Page 8: PlantUML_Class

@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 interfaceYou can also define lollipops interface on classes, using the following syntax:

bar ()- foobar ()-- foo

foo -() bar

@startuml img/classes15a.pngclass foobar ()- foo@enduml

Changing arrows directionBy 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.pngRoom o- StudientRoom *-- Chair@enduml

1/12/2011 PlantUML

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

Page 9: PlantUML_Class

You can also change directions by reversing the link:

@startuml img/classes20b.pngStudient -o RoomChair --* Room@enduml

It is also possible to change arrow direction by adding left, right, up or down keywords inside the arrow:

@startuml img/classes20c.pngfoo -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 diagramThe 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.pngtitle Simple <b>example</b>\nof title

Object <|-- ArrayList

@enduml

Association classes

1/12/2011 PlantUML

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

Page 10: PlantUML_Class

You can define association class after that a relation has been defined between two classes, like in this example:

@startuml img/classes16.pngStudent : NameStudent "0..*" - "1..*" Course(Student, Course) .. Enrollment

Enrollment : drop()Enrollment : cancel()@enduml

You can define it in another direction:

@startuml img/classes17.pngStudent : NameStudent "0..*" -- "1..*" Course(Student, Course) . Enrollment

Enrollment : drop()Enrollment : cancel()@enduml

SkinparamYou 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 PaleGreenskinparam classArrowColor SeaGreenskinparam classBorderColor SpringGreenskinparam stereotypeCBackgroundColor YellowGreen

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

Class03 o-- Class04 : agregation

@enduml

1/12/2011 PlantUML

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

Page 11: PlantUML_Class

Skinned StereotypesYou can define specific color and fonts for stereotyped classes.

@startuml img/classes18b.png

skinparam classBackgroundColor PaleGreenskinparam classArrowColor SeaGreenskinparam classBorderColor SpringGreenskinparam stereotypeCBackgroundColor YellowGreenskinparam classBackgroundColor<<Foo>> Wheatskinparam classBorderColor<<Foo>> Tomatoskinparam stereotypeCBackgroundColor<<Foo>> DimGray

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

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

@enduml

Splitting large filesSometimes, 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 pagespage 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

1/12/2011 PlantUML

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

Page 12: PlantUML_Class

Back to top Contact

1/12/2011 PlantUML

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