PGF Umlcd Manual
PGF Umlcd Manual
Yuan Xu
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.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 }
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}
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 { 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 }
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 }
\ end { tikzpicture }
\ 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 }
\ 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
\ end { tikzpicture }
4
1.3.4 Composition
\ 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 { 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>>
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 , - >] ( 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.