Skip to content

Differences between Class, Component and Package Diagrams

UML (the Unified Modeling Language) offers several diagrams for software structure. Three are often used for building blocks: class, component and package diagrams.

Component diagram. It shows how components are wired together into larger components or whole systems. A required interface appears as a half-circle, called a socket, on the component that needs it. The article Provided and Required Interface explains the notation.

Package diagram. It groups elements into packages. A package import is a directed relationship. The importing namespace adds the names of the package’s members to its own namespace. The article Package Relationships (Import, Access, Merge, Use) covers this.

Class diagram. The verified facts for this article say nothing about class diagrams. See the article UML Class Diagram.

Hierarchy. UML components and packages can both be nested. So, according to the arc42 FAQ, component diagrams and package diagrams can both show hierarchies.

Fit with the arc42 building block view. This view is hierarchical. Level 1 is the whitebox of the overall system. Level 2 and below zoom into selected blocks. The view can hold many kinds of elements, from packages and classes to components and subsystems. Every piece of source code should have a place in it. That ties the diagrams to the code structure.

A web shop’s level 1 view shows three nested components: Catalog, Orders and Payment. Orders needs a payment service, so it carries a socket for that interface. Level 2 zooms into Orders. There, a package diagram groups the order code into packages. One package imports another, so it can use the other package’s member names directly.

Because components and packages can both be nested, more than one diagram can show the same hierarchy. The facts here do not say which to prefer. Pick one that fits the blocks you show and keep it consistent across levels.

  • Every piece of source code has a place in the building block view.
  • The building block view is hierarchical: level 1 is the whitebox of the overall system, and lower levels zoom into selected blocks.
  • Where a hierarchy is drawn with components or packages, the diagram nests them.
  • A required interface on a component diagram is drawn as a socket on the component that needs it.