Class Diagrams

A class diagram is classes, what they hold and what they do, and how they relate. It starts with class and its title.

class Shapes on a canvas
Shape [abstract]
  + name: string
  + area(): number
  + draw()
Circle
  - radius: number
  + area(): number
Square
  - side: number
  + area(): number
Canvas
  + shapes: Shape[]
  + render()
Shape <|- Circle
Shape <|- Square
Canvas o- Shape: holds

The text above: Canvas holding Shapes, and Circle and Square inheriting from the abstract Shape

Settings

Setting Also Value
direction flow, orientation, layout, rankdir down (the default), right, up or left

Classes and members

A class is named on a line of its own - class Name is the same - with its members indented under it. A member with brackets is an operation and goes in the class's second compartment; any other member is a field.

A member starts with its visibility, as a symbol or a word:

Symbol Word Visibility
+ public public
- private private
# protected protected - written as the word, since a line starting with # is a comment
~ internal, package internal

static before a member underlines it and abstract sets it in italic: static count: number, abstract deposit(amount). A member that belongs to no class - written at the top rather than under one - is said.

Stereotypes

A word in brackets after a class that is not a colour, a role or muted is the class's stereotype, shown above its name: [interface], [abstract], [enum], [record], [service] - any word you need.

Relationships

The plain arrows draw associations and dependencies - Order -> Customer, Report ..> Printer - and UML's ends, written with a space on each side, draw the rest:

  • <|-, <|--, -|>, --|> - inheritance: a hollow triangle at the more general class.
  • <|.., ..|> - realisation: a dashed line with a hollow triangle at the interface.
  • *-, *--, -*, --* - composition: a filled diamond at the whole.
  • o-, o--, -o, --o - aggregation: a hollow diamond at the whole.

The more general end of each relationship - the base class, the interface, the whole - is laid out above the other. A label goes after a colon: Canvas o- Shape: holds.

A line with a count at each end writes multiplicities: Bank 1-* Account. Each end is 1, 0..1, 1..* (also 1..n) or * (also 0..*, n, many).

class Accounts
Account [abstract]
  protected balance: number
  static count: number
  abstract deposit(amount)
Saving
  - rate: number
  + addInterest()
IAudited [interface]
  + audit()
Bank
  + accounts: Account[]
Account <|- Saving
Account ..|> IAudited
Bank 1-* Account: holds

The text above: a protected field, a static one underlined, an abstract operation in italic, an interface realised by a dashed line, and Bank holding many Accounts

Packages and notes

package Name - also namespace or group - frames the classes indented under it. note Class: what to say puts a note beside a class.