Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Package diagrams

A package diagram shows how a system is grouped into packages and how those packages depend on each other. Start a file with diagram package.

diagram package "Title"
direction down

The default direction is down, which suits layered dependencies. direction right is also available. Both lines are optional except for diagram package, because a file with no header is a component diagram.

Packages

A package is drawn as a folder. When it shows its contents its name sits in the tab, and when it does not its name sits in the body, as UML 2.5.1 specifies.

diagram package "Names in the tab, or in the body"

package Empty
package Types {
  class Integer
  class Time
}
Names in the tab, or in the body Empty Types Integer Time

Packages nest to any depth, and a <<stereotype>> becomes a keyword above the name.

diagram package "Nesting and keywords"

package "Platform" as pf <<model>> {
  package "Kernel" as kernel {
    class Scheduler
    class Timer
  }
  package Drivers
  Drivers ..> kernel : <<access>>
}
package Empty
Empty left-of pf
Nesting and keywords «model» Platform Kernel Scheduler Timer Drivers Empty «access»

Classifiers

class, interface and enum declare simple name boxes. An interface carries the «interface» keyword and an enumeration the «enumeration» keyword. A declared stereotype joins that keyword list, except <<abstract>>, which sets the name in italics.

class Order
interface Repository <<abstract>>
enum Status

Relations

ArrowMeaningDrawn as
a ..> bDependencyDashed line, open head
a --> bDirected associationSolid line, open head
a -- b, a .. bPlain lineSolid or dashed, no head
a --|> bGeneralizationSolid line, hollow triangle
a ..|> bRealizationDashed line, hollow triangle
a +-- bContainmentSolid line, circle-plus on a

Reversed forms such as <.., <--, <|--, <|.. and --+ swap the two ends. A <<x>> in a label becomes «x», which is how «import», «access» and «merge» are written. Any other label text is kept as it is.

diagram package "Relations"

package App
package Lib
package Util
package Base

App ..> Lib : <<import>>
App ..> Util : <<access>>
Lib ..> Base : <<merge>>
Util ..> Base : uses
Relations App Lib Util Base «import» «access» «merge» uses
diagram package "Classifiers"
direction right

interface Shape <<abstract>>
class Circle
class Square
class Base
enum Kind

Circle ..|> Shape
Square ..|> Shape
Circle --|> Base
Base --> Kind : tags
Classifiers «interface» Shape Circle Square Base «enumeration» Kind tags

Containment

owner +-- member draws UML’s containment connector, with the circle-plus on the owner. Use it for members declared outside the owner’s block. Members in the next rank share one trunk.

diagram package "Containment"

package Types
class Shape
class Point
class Color
Types +-- Shape
Types +-- Point
Types +-- Color
Containment Types Shape Point Color
diagram package "Containment, left to right"
direction right

package "api" as api
package "core" as core
class Router
class Handler
core +-- Router
core +-- Handler
api ..> core : <<import>>
Containment, left to right api core Router Handler «import»

A dependency may run between an element and the package drawn around it, for example a nested package importing its parent. The line runs to the inside of the package boundary. Containment with +-- needs no line when the member is already drawn inside the owner, so that is an error.

diagram package "Nested import"
package Core {
  package Scheduler
  Scheduler ..> Core : <<import>>
}
Nested import Core Scheduler «import»
diagram package "Containment"
package Types { class Shape }
Types +-- Shape

drumlin says:

3:1: error: `Shape` is drawn inside `Types`, so containment needs no line; declare `Shape` outside the `Types` block

Notes and layout hints

Notes and hints work as they do in component diagrams. A note attaches to a package or a classifier and sits next to it, inside the same container. left-of, right-of, above, below and align set relative order.

diagram package "Notes and hints"

package Core {
  package Parser
  package Checker
  package Emitter
  package Runtime
  package Stdlib
  Parser left-of Checker
}
class Tool
Tool ..> Core : <<import>>
note Tool: Built separately.
Notes and hints Core Parser Checker Emitter Runtime Stdlib Tool Built separately. «import»

Siblings with no relations between them are folded into a compact block, unless a hint names them.

diagram package "Unconnected members fold into rows"

package Kernel {
  class Scheduler
  class Timer
  class Mutex
  class Channel
  class Pipe
  class Signal
}
Unconnected members fold into rows Kernel Scheduler Timer Mutex Channel Pipe Signal

Statements on one line

A semicolon separates statements, which suits small packages.

package "Core" as core { class Foo; interface IBar; enum Mode; package inner { } }