Drawing UML Class Diagram by using pgf-umlcd
Yuan Xu
May 22, 2022 (v0.3)
Abstract
pgf-umlcd is a LaTeX package for drawing UML Class Diagrams. As stated by its name, it is based on a very popular
graphic package PGF/TikZ. This document presents the usage of pgf-umlcd and collects some UML class diagrams as
examples. pgf-umlcd can be downloaded from http://code.google.com/p/pgf-umlcd/.
Contents
1 Basics
1.1 Class with attributes and operations
Note: If you don’t want to show empty parts in the diagrams, please use simplified option, e.g. \usepackage[simplified]{pgf
-umlcd}.
\ begin { tikzpicture }
\ begin { class }[ text width =8 cm ]{ ClassName }{0 ,0}
\ attribute { name : attribute type }
ClassName \ attribute { name : attribute type = default value }
name : attribute type
\ operation { name ( parameter list ) : type of value
name : attribute type = default value returned }
% virtual o p e r a t i o n
name(parameter list) : type of value returned \ operation [0]{ name ( parameters list ) : type of
name(parameters list) : type of value returned value returned }
\ end { class }
\ end { tikzpicture }
1.1.1 Visibility of attributes and operations
\ begin { tikzpicture } % [ show b a c k g r o u n d grid ]
Class \ begin { class }[ text width =7 cm ]{ Class }{0 ,0}
+ Public \ attribute {+ Public }
\ attribute {\# Protected }
# Protected \ attribute { - Private }
- Private \ attribute {$\ sim $ Package }
∼ Package \ end { class }
\ begin { class }[ text width =7 cm ]{ BankAccount }{0 , -3}
\ attribute {+ owner : String }
BankAccount \ attribute {+ balance : Dollars }
+ owner : String
\ operation {+ deposit ( amount : Dollars ) }
+ balance : Dollars \ operation {+ withdrawal ( amount : Dollars ) }
+ deposit( amount : Dollars ) \ operation {\# updateBalance ( newBalance : Dollars
+ withdrawal( amount : Dollars ) )}
\ end { class }
# updateBalance( newBalance : Dollars ) \ end { tikzpicture }
1
1.1.2 Abstract class and interface
\ begin { tikzpicture }
\ begin { abstractclass }[ text width =5 cm ]{ BankAccount
<<abstract>> }{0 ,0}
BankAccount \ attribute { owner : String }
owner : String \ attribute { balance : Dollars = 0}
balance : Dollars = 0
\ operation { deposit ( amount : Dollars ) }
deposit(amount : Dollars)
\ operation [0]{ withdrawl ( amount : Dollars ) }
withdrawl(amount : Dollars) \ end { abstractclass }
\ end { tikzpicture }
\ begin { tikzpicture } % [ show b a c k g r o u n d grid ]
<<interface>> \ begin { interface }{ Person }{0 ,0}
Person \ attribute { firstName : String }
firstName : String \ attribute { lastName : String }
lastName : String \ end { interface }
\ end { tikzpicture }
1.1.3 Object
\ begin { tikzpicture }
\ begin { object }[ text width =6 cm ]{ Instance Name }{0 ,0}
Instance Name: Class Name \ instanceOf { Class Name }
\ attribute { attribute name = value }
attribute name = value \ end { object }
\ end { tikzpicture }
Note: Object with rounded corners and methods are used in German school for didactic reasons. You get the rounded corners
with \usepackage[school]{pgf-umlcd}. If you need both in one document you can switch it with \switchUmlcdSchool
\ begin { tikzpicture }
\ begin { object }[ text width =6 cm ]{ Instance Name }{0 ,0}
Instance Name: Class Name \ instanceOf { Class Name }
\ attribute { attribute name = value }
attribute name = value \ end { object }
\ end { tikzpicture }
\ begin { tikzpicture }
\ begin { object }[ text width =6 cm ]{ Thomas ’ account
}{0 ,0}
Thomas’ account: BankAccount \ instanceOf { BankAccount }
owner = Thomas \ attribute { owner = Thomas }
balance = 100 \ attribute { balance = 100}
deposit(amount : Dollars) \ operation { deposit ( amount : Dollars ) }
withdrawl(amount : Dollars) \ operation [0]{ withdrawl ( amount : Dollars ) }
\ end { object }
\ end { tikzpicture }
1.1.4 Note
The \umlnote use the same syntax as tikz command \node, e.g. \umlnote[style] (name)at (coordinate){text};
\ begin { tikzpicture }
\ umlnote ( note ) { This is a note .};
This is a note. \ end { tikzpicture }
2
1.2 Inheritance and implement
1.2.1 Inheritance
\ begin { tikzpicture }
\ begin { class }[ text width =5 cm ]{ BankAccount }{0 ,0}
\ attribute { owner : String }
\ attribute { balance : Dollars = 0}
\ operation { deposit ( amount : Dollars ) }
\ operation [0]{ withdrawl ( amount : Dollars ) }
\ end { class }
BankAccount
owner : String \ begin { class }[ text width =7 cm ]{ C he c ki ng Ac c ou nt
balance : Dollars = 0 }{ -5 , -5}
deposit(amount : Dollars)
withdrawl(amount : Dollars)
\ inherit { BankAccount }
\ attribute { i n s u f f i c i e n t F u n d s F e e : Dollars }
\ operation { processCheck ( ch eckToPro cess : Check
)}
\ operation { withdrawal ( amount : Dollars ) }
CheckingAccount SavingsAccount
insufficientFundsFee : Dollars annualInteresRate : Percentage
\ end { class }
processCheck ( checkToProcess : Check ) depositMonthlyInterest ( )
withdrawal ( amount : Dollars ) withdrawal ( amount : Dollars ) \ begin { class }[ text width =7 cm ]{ Saving sAccoun t }{5 , -5}
\ inherit { BankAccount }
\ attribute { a n n u a l I n t e r e s R a t e : Percentage }
\ operation { d e p o s i t M o n t h l y I n t e r e s t ( ) }
\ operation { withdrawal ( amount : Dollars ) }
\ end { class }
\ end { tikzpicture }
1.2.2 Multiple Inheritance
\ begin { tikzpicture } % [ show b a c k g r o u n d grid ]
\ begin { class }[ text width = 2 cm ]{ TArg }{0 , 0}
\ end { class }
\ begin { class }[ text width = 2 cm ]{ TGroup }{5 , 0}
TArg TGroup TProgInit \ end { class }
\ begin { class }[ text width = 2 cm ]{ TProgInit }{10 , 0}
\ end { class }
TProgram
\ begin { class }[ text width = 2 cm ]{ TProgram }{5 , -2}
\ inherit { TProgInit }
\ inherit { TGroup }
\ inherit { TArg }
\ end { class }
\ end { tikzpicture }
1.2.3 Implement an interface
\ begin { tikzpicture } % [ show b a c k g r o u n d grid ]
\ begin { interface }{ Person }{0 ,0}
\ attribute { firstName : String }
<<interface>>
Person \ attribute { lastName : String }
firstName : String \ end { interface }
lastName : String
\ begin { class }{ Professor }{ -5 , -5}
\ implement { Person }
\ attribute { salary : Dollars }
\ end { class }
\ begin { class }{ Student }{5 , -5}
Professor Student
\ implement { Person }
salary : Dollars major : String
\ attribute { major : String }
\ end { class }
\ end { tikzpicture }
3
1.3 Association, Aggregation and Composition
1.3.1 Association
\ begin { tikzpicture }
\ begin { class }[ text width =7 cm ]{ Flight }{0 ,0}
\ attribute { flightNumber : Integer }
\ attribute { departureTime : Date }
\ attribute { f lightDu ration : Minutes }
\ attribute { d e p a r t i n g A i r p o r t : String }
\ attribute { ar r iv in gA i rp or t : String }
\ operation { delayFlight ( nu mb er O fM in ut e s :
Flight Plane Minutes ) }
flightNumber : Integer airPlaneType : String \ operation { g etArriv alTime ( ) : Date }
departureTime : Date assignedFlights 0..1 maximumSpeed : MPH
flightDuration : Minutes maximumDistance : Miles \ end { class }
0..* assignedPlane
departingAirport : String tailID : String
arrivingAirport : String
\ begin { class }{ Plane }{11 ,0}
delayFlight ( numberOfMinutes : Minutes )
getArrivalTime ( ) : Date
\ attribute { airPlaneType : String }
\ attribute { maximumSpeed : MPH }
\ attribute { ma x im um Di s ta nc e : Miles }
\ attribute { tailID : String }
\ end { class }
\ association { Plane }{ assignedPlane }{0..1}{ Flight
}{0..*}{ as s ig ne dF l ig ht s }
\ end { tikzpicture }
1.3.2 Unidirectional association
\ begin { tikzpicture }
% \ draw [ help lines ] ( -7 , -6) grid (6 ,0) ;
\ begin { class }[ text width =6 cm ]{
O v e r d r a w n A c c o u n t s R e p o r t }{0 ,0}
\ attribute { generatedOn : Date }
\ operation { refresh ( ) }
\ end { class }
OverdrawnAccountsReport BankAccount
generatedOn : Date overdrawnAccounts owner : String \ begin { class }{ BankAccount }{12 ,0}
balance : Dollars
refresh ( ) 0..* \ attribute { owner : String }
deposit(amount : Dollars)
withdrawl(amount : Dollars) \ attribute { balance : Dollars }
\ operation { deposit ( amount : Dollars ) }
\ operation [0]{ withdrawl ( amount : Dollars ) }
\ end { class }
\ unidirectionalAssociation { OverdrawnAccountsReport
}{ o v e r d r a w n A c c o u n t s }{0..*}{ BankAccount }
\ end { tikzpicture }
1.3.3 Aggregation
\ begin { tikzpicture }[ show background grid ]
\ begin { class }{ Car }{0 ,0}
\ end { class }
Car wheels Wheel \ begin { class }{ Wheel }{7.5 ,0}
4 \ end { class }
\ aggregation { Car }{ wheels }{4}{ Wheel }
\ end { tikzpicture }
4
1.3.4 Composition
\ begin { tikzpicture }[ show background grid ]
\ begin { class }{ Company }{0 ,0}
\ end { class }
\ begin { class }{ Department }{10 ,0}
Company theDepartment Department \ end { class }
1..*
\ composition { Company }{ theDepartment }{1..*}{
Department }
\ end { tikzpicture }
1.4 Package
\ begin { tikzpicture }
\ begin { package }{ Accounts }
\ begin { class }[ text width =5 cm ]{ BankAccount }{0 ,0}
\ attribute { owner : String }
\ attribute { balance : Dollars = 0}
\ operation { deposit ( amount : Dollars ) }
\ operation [0]{ withdrawl ( amount : Dollars ) }
\ end { class }
Accounts
\ begin { class }[ text width =7 cm ]{ C he ck i ng Ac co u nt
BankAccount
owner : String
}{ -5 , -5}
balance : Dollars = 0 \ inherit { BankAccount }
deposit(amount : Dollars)
withdrawl(amount : Dollars)
\ attribute { i n s u f f i c i e n t F u n d s F e e : Dollars }
\ operation { processCheck ( che ckToProc ess :
Check ) }
\ operation { withdrawal ( amount : Dollars ) }
CheckingAccount SavingsAccount \ end { class }
insufficientFundsFee : Dollars annualInteresRate : Percentage
processCheck ( checkToProcess : Check ) depositMonthlyInterest ( ) \ begin { class }[ text width =7 cm ]{ Saving sAccount
withdrawal ( amount : Dollars ) withdrawal ( amount : Dollars )
}{5 , -5}
\ inherit { BankAccount }
\ attribute { a n n u a l I n t e r e s R a t e : Percentage }
\ operation { d e p o s i t M o n t h l y I n t e r e s t ( ) }
\ operation { withdrawal ( amount : Dollars ) }
\ end { class }
\ end { package }
\ end { tikzpicture }
2 Customization
2.1 Color settings
The color of digram is defined by \umltextcolor, \umldrawcolor and \umlfillcolor, such as:
5
\ renewcommand {\ umltextcolor }{ red }
\ renewcommand {\ umlfillcolor }{ green }
\ renewcommand {\ umldrawcolor }{ blue }
\ begin { tikzpicture }
ClassName \ begin { class }[ text width =8 cm ]{ ClassName }{0 ,0}
name : attribute type \ attribute { name : attribute type }
\ attribute { name : attribute type = default value }
name : attribute type = default value
\ operation { name ( parameter list ) : type of value
name(parameter list) : type of value returned returned }
name(parameters list) : type of value returned % virtual o p e r a t i o n
\ operation [0]{ name ( parameters list ) : type of
value returned }
\ end { class }
\ end { tikzpicture }
3 Examples
3.1 Abstract Factory
<<interface>>
AbstractFactory <<import>> Client
+ CreateProductA()
+ CreateProductB()
<<interface>> <<import>>
AbstractProductA
ConcreteFactory2 ConcreteFactory1
<<instantiate>>
+ CreateProductA() + CreateProductA() ProductA1 ProductA2
+ CreateProductB() + CreateProductB()
<<instantiate>>
<<interface>> <<import>>
AbstractProductB
<<instantiate>> ProductB1 ProductB2
<<instantiate>>
\ begin { tikzpicture }[ show background grid ]
\ begin { interface }{ A b st ra ct F ac to ry }{0 ,0}
\ operation [0]{+ Create Product A () }
\ operation [0]{+ Create Product B () }
\ end { interface }
\ begin { class }{ Co nc re t eF ac to r y 2}{ -3 , -4}
\ implement { Ab s tr ac tF a ct or y }
\ operation {+ Cre ateProd uctA () }
\ operation {+ Cre ateProd uctB () }
\ end { class }
\ begin { class }{ Co nc re t eF ac to r y 1}{3 , -4}
\ implement { Ab s tr ac tF a ct or y }
\ operation {+ Cre ateProd uctA () }
\ operation {+ Cre ateProd uctB () }
\ end { class }
\ begin { interface }{ A b s t r a c t Pr o d u c t A }{15 , -2}
\ end { interface }
\ begin { class }{ ProductA 1}{12 , -5}
\ implement { A b s t r a c t P r o d u c t A }
\ end { class }
6
\ begin { class }{ ProductA 2}{18 , -5}
\ implement { A b s t r a c t P r o d u c t A }
\ end { class }
\ draw [ umlcd style dashed line , - >] ( C on c re te Fa c to ry 1) -- node [ above ,
sloped , black ]{$ < <$ instantiate $ > >$} ( ProductA 1) ;
\ draw [ umlcd style dashed line , - >] ( C on c re te Fa c to ry 2. south ) ++
(1 ,0) -- ++(0 , -1) -- node [ above , sloped ,
black ]{$ < <$ instantiate $ > >$} ++(20 ,0) -| ( ProductA 2) ;
\ begin { interface }{ A b s t r a c t Pr o d u c t B }{15 , -8}
\ end { interface }
\ begin { class }{ ProductB 1}{12 , -11}
\ implement { A b s t r a c t P r o d u c t B }
\ end { class }
\ begin { class }{ ProductB 2}{18 , -11}
\ implement { A b s t r a c t P r o d u c t B }
\ end { class }
\ draw [ umlcd style dashed line , - >] ( C on c re te Fa c to ry 1) | - node [ above ,
sloped , black ]{$ < <$ instantiate $ > >$} ( ProductB 1) ;
\ draw [ umlcd style dashed line , - >] ( C on c re te Fa c to ry 2. south ) ++
( -1 ,0) -- ++(0 , -7) -- node [ above , sloped ,
black ]{$ < <$ instantiate $ > >$} ++(20 ,0) -| ( ProductB 2) ;
\ begin { class }{ Client }{22 , -0.5}
\ end { class }
\ draw [ umlcd style dashed line , - >] ( Client ) -- node [ above , sloped ,
black ]{$ < <$ import $ > >$} ( Ab s tr ac tF a ct or y ) ;
\ draw [ umlcd style dashed line , - >] ( Client ) | - node [ above , sloped ,
black ]{$ < <$ import $ > >$} ( A b s t r a c t P r o d u c t A ) ;
\ draw [ umlcd style dashed line , - >] ( Client ) | - node [ above , sloped ,
black ]{$ < <$ import $ > >$} ( A b s t r a c t P r o d u c t B ) ;
\ end { tikzpicture }
4 Acknowledgements
Many people contributed to pgf-umlcd by reporting problems, suggesting various improvements or submitting code. Here is
a list of these people: Martin Quinson, Johannes Pieper, sh w, and Maarten van Dessel.