UML class diagram examples
Two domains modelled properly, and an argument for every line on them - a library that needs inheritance and a basket that needs composition.
6 min readUML 2.5.15 of 35
The short answer
- Two domains cover every relationship kind you will draw: a library that needs inheritance and a basket that needs composition.
- A diagram is a view of a model, not the model. A view that contains every class answers nothing.
- Five to twelve classes. Past twelve the relationships start crossing and attention goes into following lines instead of the domain.
- Draw the classes the current question is about and leave the other two hundred in the model, where a tool can still find them.
01A library, where inheritance earns its place#
The diagram above is worth reading relationship by relationship. Book and DVD generalize LibraryItem, which is drawn as a hollow triangle pointing at the parent - and the reason to draw it that way rather than as two unrelated classes with duplicated attributes is the association below it. Loan points at exactly one LibraryItem, and because it points at the abstract parent it can lend either subtype without knowing which.
That is the whole test for inheritance in a domain model: not "do these two things feel similar", but is there somewhere in the model that wants to hold either one without caring which. If nothing does, two separate classes are the smaller and more honest answer.
Note the multiplicities on the Member-to-Loan association: 1 to 0..*. A member with no loans is a member; a loan with no member is a bug. That asymmetry is a real fact about the domain and it is stated on the line rather than left to a comment. The class diagram article covers how each of those symbols is drawn.
02A basket, where composition earns its place#
This one contains no inheritance at all, and that is the point of showing it beside the library. What it has instead is a composition and an interface, and each is there for a reason the other diagram had no use for.
The filled diamond between Basket and BasketLine is a claim: delete the basket and the lines go with it, because a basket line is meaningless on its own. Compare it with the plain association from BasketLine to Product, where the product obviously outlives the line - the same shape of relationship, two different lifetimes, and the notation distinguishes them.
PricingRule is the extension point. Two realizations are drawn because one realization is not an abstraction, it is a class with an extra step - and the moment there are two, adding a third promotion is a new class rather than a new branch in an existing method.
03How to read either one in thirty seconds#
Both diagrams reward the same reading order, and it is not left to right. Start with the multiplicities, because they are the decisions: they say what the system permits and they are the hardest thing to change later. Then read the diamonds, which say what gets deleted with what. Then the triangles, which say what can stand in for what. Attributes last, and only the ones you doubt.
A diagram that survives that reading is worth keeping. One where the multiplicities are blank is not finished, however many attributes it lists - and that is by far the most common state to find a class diagram in.
In one line each
- 01Draw inheritance when something in the model holds the parent type without caring which subtype it has.
- 02Use composition when the part is deleted with the whole, and plain association otherwise.
- 03An interface with one realization is a class with extra steps; two makes it an extension point.
- 04Multiplicities are the decisions on the diagram. Blank ones mean the diagram is unfinished.
- 05Five to twelve classes per diagram. A view holding everything answers nothing.
Next: how to draw one from scratch, and the symbol reference for checking a mark you are unsure of.
04Common questions#
What is a good example of a UML class diagram?
One small enough to read in a single glance and complete enough to argue with. A library with an abstract LibraryItem and two subtypes shows inheritance; a basket composed of its lines shows composition. Between them they exercise every relationship kind you are likely to draw.
Should a class diagram show every class in the system?
No. A diagram is a view of a model, not the model itself, and a view that contains everything answers nothing. Draw the classes that the question at hand is about, and leave the other two hundred in the model where a tool can still find them.
How many classes belong on one class diagram?
Between five and about twelve. Below five you usually have not said enough to be worth drawing; above twelve the relationships start crossing and the reader spends their attention following lines instead of understanding the domain.
In this series
- 01What is UML?
- 02UML symbols
- 03Choosing a diagram
- 04Class diagrams
- 05Class diagram examples
- 06How to draw a class diagram
- 07Class diagram symbols
- 08Sequence diagrams
- 09Sequence diagram examples
- 10How to draw a sequence diagram
- 11Use case diagrams
- 12Use case examples
- 13Activity diagrams
- 14Activity examples
- 15State machine diagrams
- 16State machine examples
- 17Component diagrams
- 18Component examples
- 19Drawing a component diagram
- 20Component symbols
- 21Deployment diagrams
- 22Deployment examples
- 23Object diagrams
- 24Package diagrams
- 25Composite structure diagrams
- 26Communication diagrams
- 27Sequence vs communication
- 28Timing diagrams
- 29Interaction overview diagrams
- 30Profile diagrams
- 31UML with AI
- 32E-commerce example
- 33Banking example
- 34Microservices example
- 35AWS example
Related reading
Structure diagrams
Structure diagrams
Notation reference
Foundations
Foundations
Behaviour diagrams