System Design Academy

Saving…

Lesson 9 of 10 in Module 10 of 45 lessons complete

UML Class Diagrams

Module 1, lesson 9Concept12 min

Definition

UML

In plain English
UML is a standard set of diagram symbols for describing software designs.
Think of it like
It is like map symbols, where everyone reads a blue line as a river.

Class diagram

In plain English
A class diagram shows types, their fields and methods, and how they relate.
Think of it like
It is like a family tree, but for the types in your code.

Why it exists

You can discuss a station design on a whiteboard in two minutes before writing 200 lines. A shared diagram makes the types and their relationships visible to your teammate.

Interviews use this shorthand too. Show important fields, methods and replaceable parts. Check that your code matches the picture.

How it works

Visibility marker

In plain English
A visibility marker is the plus or minus before a member, which maps to exported or unexported names in Go.
Think of it like
It is like public entrance and staff-only signs on doors.
  • Put the type name at the top, followed by fields and methods.
  • Use + for exported members and - for unexported members.
  • Mark a contract with <<interface>>.
  • Write counts at relationship ends, such as 1 and many. This is multiplicity.
RelationshipArrowMeaning
Association-->Keeps a link
Aggregationo--Groups independent parts
Composition*--Owns parts
Interface implementation..|>Satisfies an interface
Dependency..>Uses temporarily
Inheritance<|--Child class extends parent; Go has no classes
UMLGo
ClassStruct plus methods
InterfaceInterface
+ memberUppercase initial
- memberLowercase initial
InheritanceEmbedding reuses code; interfaces let different types answer the same calls
Mermaid source for the station diagram.
Text
classDiagram
 class Station {
 -string id
 +Connect()
 }
 Station "1" *-- "many" Connector

Station has an unexported id and an exported Connect method. One Station owns many Connector values.

One station owns many connectors.

Station has an unexported id and an exported Connect method. One Station owns many Connector values.

Examples

Vary station pricing

A station owns connectors. A charging session links to a connector and driver, and obtains prices through a policy.

Station owns Connector; Session links to Connector, Driver and PricingPolicy. FlatPricing and TimeOfDayPricing satisfy PricingPolicy's Price method.

A pricing interface marks where charging behaviour can vary.

Station owns Connector; Session links to Connector, Driver and PricingPolicy. FlatPricing and TimeOfDayPricing satisfy PricingPolicy's Price method.

FlatPricing and TimeOfDayPricing provide the same methods. Session uses either through PricingPolicy.

The insight

Interfaces in the diagram show where behaviour can vary.

Trade-offs

Pros

  • Fast shared understanding.
  • Catch structural flaws before coding.

Cons

  • Pictures can drift away from code.
  • Drawing every read method clutters the picture.

Use when

  • Interviews and new modules.
  • Onboarding someone to an unfamiliar design.

Avoid when

  • Trivial code.
  • Drawing every field and read method without a design question.

Quick check

A filled diamond on the Station end of Station—Connector means…
How does a UML - field appear in Go?

Takeaways

  • Class diagrams show structure.
  • Learn six arrows and +/-.
  • Diagrams should map directly onto Go types.

Try the quick check above whenever you feel ready.

Previous: Object Relationships