Crash Course · Beginner → Advanced
UML Diagrams: The Complete Crash Course with Examples
Learn the Unified Modeling Language step by step using one example system (LibraryHub): what UML is, structural vs behavioural diagrams, and how to read and draw use case, class, object, sequence, activity, state machine, component and deployment diagrams — with PlantUML and Mermaid code you can copy.
Beginner → Advanced15 min readInterview Q&A
What Is UML?
UML 2.x defines 14 diagram types. In practice, most teams use about six: use case, class, sequence, activity, state machine and component/deployment.
Structural vs Behavioural Diagrams
| Structural — what the system is | Behavioural — what the system does |
|---|---|
| Class, Object, Component, Deployment, Package, Composite structure, Profile | Use case, Activity, State machine, and Interaction diagrams: Sequence, Communication, Timing, Interaction overview |
Use Case Diagram
Shows who (actors) uses the system for what (use cases). Drawn early, from the SRS.
- Actor (stick figure) — a role outside the system: Member, Librarian, Email Service.
- Use case (oval) — a goal: Search Book, Borrow Book.
- System boundary (rectangle) — what is inside the system.
- <<include>> — always performed as part of another (Borrow Book includes Log In).
- <<extend>> — optional or conditional behaviour (Pay Fine extends Return Book when overdue).
- Generalisation — Librarian inherits from Staff.
@startuml
left to right direction
actor Member
actor Librarian
rectangle LibraryHub {
Member -- (Search Book)
Member -- (Borrow Book)
Member -- (Return Book)
Librarian -- (Manage Catalogue)
(Borrow Book) .> (Log In) : include
(Pay Fine) .> (Return Book) : extend
}
@enduml
Class Diagram
The most important structural diagram: classes, their attributes, operations and relationships. It maps directly to object-oriented code.
Read it as: one Member has 0 to 5 Loans; each Loan refers to exactly 1 Book; a Book can appear in many loans over time.
| Notation | Meaning |
|---|---|
+ / - / # / ~ | public / private / protected / package visibility |
1, 0..1, *, 1..*, 0..5 | Multiplicity: how many objects take part |
Class Relationships
| Relationship | Symbol | Meaning | Example |
|---|---|---|---|
| Association | Solid line | Objects are connected | Member — Loan |
| Aggregation | Hollow diamond | Whole–part; part can exist alone | Library ◇— Book |
| Composition | Filled diamond | Strong whole–part; part dies with the whole | Book ◆— Page |
| Inheritance (generalisation) | Hollow triangle arrow | “is a” | Librarian ▷ Staff |
| Dependency | Dashed arrow | Uses temporarily | LoanService ⇢ EmailService |
| Realisation | Dashed line, hollow triangle | Implements an interface | CardPayment ⇢▷ PaymentMethod |
@startuml
class Member {
-memberId: int
-name: String
+borrow(b: Book)
}
class Loan {
-dueDate: Date
+calcFine(): Money
}
class Book {
-isbn: String
-title: String
}
abstract class Staff
class Librarian
Staff <|-- Librarian
Member "1" -- "0..5" Loan
Loan "*" -- "1" Book
@endumlAn object diagram is a snapshot of real instances, e.g. sara : Member linked to loan42 : Loan.
Sequence Diagram
Shows how objects interact over time for one scenario. Time runs top to bottom.
- Lifeline — vertical dashed line for each participant.
- Synchronous message — solid arrow with filled head; return — dashed arrow.
- Activation bar — when the object is working.
- Combined fragments —
alt(if/else),opt(optional),loop.
sequenceDiagram
actor M as Member
participant UI as Web App
participant LS as LoanService
participant DB as Database
M->>UI: Click Borrow (bookId)
UI->>LS: borrow(memberId, bookId)
LS->>DB: check loans and availability
DB-->>LS: 3 loans, book available
alt limit not reached
LS->>DB: insert loan (due +14 days)
LS-->>UI: success, due date
UI-->>M: Show confirmation
else limit reached
LS-->>UI: error
UI-->>M: Show limit message
end
Activity Diagram
A flowchart of a process or workflow, good for business processes and complex logic.
- Filled circle = start; circle with ring = end; rounded rectangle = action.
- Diamond = decision/merge with guards like
[available]. - Thick bar = fork/join for parallel actions.
- Swimlanes show who does each step.
@startuml
start
:Member returns book;
if (Overdue?) then (yes)
:Calculate fine;
:Member pays fine;
else (no)
endif
fork
:Update book status;
fork again
:Notify next reservation;
end fork
stop
@enduml
State Machine Diagram
Shows the life cycle of one object — the states it can be in and the events that move it between them.
stateDiagram-v2
[*] --> Available
Available --> OnLoan: borrow
OnLoan --> Available: return [no reservation]
OnLoan --> Reserved: return [reservation exists]
Reserved --> OnLoan: borrow by reserver
Available --> Lost: reported lost
Lost --> [*]Use it for objects with clear statuses: orders, payments, tickets, books.
Component and Deployment Diagrams
- Component diagram — major software parts and their interfaces: Web UI → Loan API → Catalogue API → Notification component → Payment gateway.
- Deployment diagram — where software runs: user browser → load balancer → 2 application servers (Docker) → PostgreSQL server; SMTP service in the cloud.
These help DevOps, security and capacity planning.
Which Diagram When?
| Question | Diagram |
|---|---|
| Who uses the system for what? | Use case |
| What are the data objects and relationships in code? | Class |
| How do parts interact in one scenario? | Sequence |
| What are the steps of a workflow? | Activity |
| What statuses does an object go through? | State machine |
| How is the software split and deployed? | Component / deployment |
| How is the database structured? | ER diagram (not UML, but used alongside) |
UML Tools
- draw.io (diagrams.net) — free drag-and-drop.
- PlantUML — diagrams from text, great with version control.
- Mermaid — text diagrams rendered in GitHub, GitLab, Notion and many docs tools.
- Lucidchart, Miro — collaborative online diagrams.
- StarUML, Visual Paradigm, Enterprise Architect — full modelling tools with code generation.
To try the code above: paste PlantUML into the PlantUML web server or an IDE plugin, and Mermaid into the Mermaid Live Editor or a GitHub markdown file inside a ```mermaid block.
Key Takeaways
- UML = standard visual language for software.
- Structural (class, component, deployment) vs behavioural (use case, sequence, activity, state).
- Use case: actors, goals, include (always), extend (optional).
- Class: visibility, multiplicity, association, aggregation, composition, inheritance.
- Sequence: messages over time; activity: workflow; state: object life cycle.
- Keep diagrams simple and purposeful.
Interview Questions
What is UML?
The Unified Modeling Language, an OMG standard visual language for specifying, designing and documenting software systems.
What is the difference between structural and behavioural diagrams?
Structural diagrams show the static parts of a system such as classes and components; behavioural diagrams show dynamic behaviour such as interactions, workflows and state changes.
What is the difference between include and extend in a use case diagram?
Include is behaviour always performed as part of the base use case; extend is optional behaviour added under certain conditions.
What is the difference between aggregation and composition?
Both are whole–part relationships; in aggregation the part can exist independently, in composition the part’s life depends on the whole.
What does multiplicity 0..* mean?
Zero or more objects can be associated.
When would you use a sequence diagram?
To show how objects or services exchange messages in time order for a specific scenario, like a login or checkout.
How is an activity diagram different from a sequence diagram?
An activity diagram shows the flow of steps and decisions in a process; a sequence diagram shows message exchanges between participants over time.
What is a state machine diagram used for?
To model the states an object goes through during its life and the events that trigger transitions.
Exam Quick Revision
- UML: 14 diagrams; structural vs behavioural.
- Use case: actor, use case, boundary, include, extend.
- Visibility: + public, - private, # protected, ~ package.
- Multiplicity: 1, 0..1, *, 1..*.
- Aggregation ◇ (independent part); composition ◆ (dependent part).
- Inheritance: hollow triangle; realisation: dashed + triangle.
- Sequence = time order; activity = workflow; state = life cycle.
- Tools: draw.io, PlantUML, Mermaid.


