UML Class Diagrams
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.
| Relationship | Arrow | Meaning |
|---|---|---|
| Association | --> | Keeps a link |
| Aggregation | o-- | Groups independent parts |
| Composition | *-- | Owns parts |
| Interface implementation | ..|> | Satisfies an interface |
| Dependency | ..> | Uses temporarily |
| Inheritance | <|-- | Child class extends parent; Go has no classes |
| UML | Go |
|---|---|
| Class | Struct plus methods |
| Interface | Interface |
| + member | Uppercase initial |
| - member | Lowercase initial |
| Inheritance | Embedding reuses code; interfaces let different types answer the same calls |
classDiagram
class Station {
-string id
+Connect()
}
Station "1" *-- "many" ConnectorStation 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.
FlatPricing and TimeOfDayPricing provide the same methods. Session uses either through PricingPolicy.
The insight
Interfaces in the diagram show where behaviour can vary.
From diagram to Go types
An auction owns lots. Each lot groups bids saved in the bidding history. Each bid links to its bidder.
Auction owns Lot values, each grouping Bid records linked to a Bidder. Auction also uses BidRule, whose Accept method takes a Bid and returns an error.
package auction
import "fmt"
type Bidder struct{ ID string }
type Bid struct {
Bidder *Bidder
Amount int
}
type Lot struct{ bids []*Bid }
type BidRule interface{ Accept(b Bid) error }
type Auction struct {
lots []Lot
rule BidRule
}
func NewAuction(rule BidRule) *Auction { return &Auction{lots: []Lot{{}}, rule: rule} }
type PositiveBid struct{}
func (PositiveBid) Accept(b Bid) error {
if b.Amount <= 0 {
return fmt.Errorf("bid must be positive")
}
return nil
}
The insight
A good diagram maps almost line-for-line to Go types.
A route needs two stops
A route owns at least two stops. A bus follows that route. A driver links to the bus, and a parent looks up the route.
One Route owns at least two Stops. Bus links to Route, Driver links to Bus, and Parent temporarily uses Route.
The 2..* label means at least two stops. Check that count when creating the route; Go does not enforce diagram labels.
The insight
Multiplicity captures business rules such as a route having at least two stops.
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
Takeaways
- Class diagrams show structure.
- Learn six arrows and +/-.
- Diagrams should map directly onto Go types.