Use Case, Activity, and Sequence Diagrams
In Lecture 1, we learned about structural diagrams:
| Structural Diagrams | Behavioral Diagrams |
|---|---|
| Class Diagram | Use Case Diagram |
| Object Diagram | Activity Diagram |
| Component Diagram | Sequence Diagram |
| Package Diagram | State Machine Diagram |
| Deployment Diagram | Communication Diagram |
Today we focus on the three most common behavioral diagrams (in bold)
A use case diagram shows what a system does from a user's perspective.
| Element | Symbol | Description |
|---|---|---|
| Actor | Stick figure | Someone or something that interacts with the system |
| Use Case | Oval/Ellipse | A specific functionality or action the system provides |
| System Boundary | Rectangle | Defines the scope of the system |
| Association | Solid line | Connects actors to use cases they can perform |
flowchart LR
subgraph System["Online Shop System"]
UC1(["Browse Products"])
UC2(["Add to Cart"])
UC3(["Checkout"])
UC4(["Make Payment"])
UC3 -.->|include| UC4
end
Customer[/Customer\]
Customer --- UC1
Customer --- UC2
Customer --- UC3
Customer --- UC4
| Relationship | Notation | Meaning |
|---|---|---|
| Include | «include» → | Use case A always includes use case B |
| Extend | «extend» → | Use case B optionally extends use case A |
| Generalization | ──▷ | One actor/use case inherits from another |
Create a use case diagram for a Library Management System with these requirements:
An activity diagram shows the flow of activities in a process.
| Element | Symbol | Description |
|---|---|---|
| Initial Node | ● (filled circle) | Starting point of the flow |
| Final Node | ⊙ (circle with border) | End point of the flow |
| Activity | Rounded rectangle | An action or task to perform |
| Decision | ◇ (diamond) | Branch based on condition |
| Merge | ◇ (diamond) | Joins multiple paths into one |
| Fork | Thick horizontal bar | Splits into parallel activities |
| Join | Thick horizontal bar | Waits for all parallel activities |
flowchart TD
Start((●)) --> ReceiveOrder["Receive Order"]
ReceiveOrder --> CheckStock{In Stock?}
CheckStock -->|No| CancelOrder["Cancel Order"]
CancelOrder --> End1((⊙))
CheckStock -->|Yes| Fork[/ /]
Fork --> PackItems["Pack Items"]
Fork --> ProcessPayment["Process Payment"]
PackItems --> Join[\ \]
ProcessPayment --> Join
Join --> ShipOrder["Ship Order"]
ShipOrder --> End2((⊙))
Swimlanes partition activities by responsibility (actor, department, system).
flowchart TD
subgraph Customer
A((●)) --> B["Place Order"]
G["Receive Order"] --> H((⊙))
end
subgraph System
C["Verify Stock"]
F["Send Notification"]
end
subgraph Warehouse
D["Pack Order"]
E["Ship Order"]
end
B --> C
C --> D
D --> E
E --> F
F --> G
Use guard conditions in brackets to control flow:
flowchart TD
CheckLogin["Check Login"] --> Decision{Valid?}
Decision -->|"[invalid]"| ShowError["Show Error"]
Decision -->|"[valid]"| ShowDashboard["Show Dashboard"]
ShowError --> Merge{" "}
ShowDashboard --> Merge
Merge --> LogEvent["Log Event"]
Create an activity diagram for an ATM cash withdrawal process:
A sequence diagram shows how objects interact over time.
| Element | Description |
|---|---|
| Lifeline | Vertical dashed line representing an object's existence over time |
| Activation Box | Rectangle on lifeline showing when object is active/processing |
| Synchronous Message | Solid arrow with filled head (→) - sender waits for response |
| Return Message | Dashed arrow (- - →) - response to synchronous message |
| Asynchronous Message | Solid arrow with open head (→) - sender doesn't wait |
| Self-call | Arrow that loops back to the same lifeline |
sequenceDiagram
actor User
participant LoginForm
participant AuthService
participant Database
User->>LoginForm: enter credentials
LoginForm->>AuthService: validate()
AuthService->>Database: findUser()
Database-->>AuthService: user data
AuthService->>AuthService: checkPassword
AuthService-->>LoginForm: auth result
LoginForm-->>User: show result
Fragments add control flow logic to sequence diagrams:
| Fragment | Meaning | Use Case |
|---|---|---|
| alt | Alternative (if-else) | Different paths based on condition |
| opt | Optional (if) | Executes only if condition is true |
| loop | Loop | Repeats messages while condition holds |
| par | Parallel | Concurrent execution |
| ref | Reference | References another sequence diagram |
sequenceDiagram
participant Client
participant Server
participant Database
Client->>Server: request()
Server->>Database: query()
alt data found
Database-->>Server: data
Server-->>Client: success
else data not found
Database-->>Server: null
Server-->>Client: error 404
end
sequenceDiagram
participant ShoppingCart
participant ProductService
participant Inventory
loop for each item in cart
ShoppingCart->>ProductService: getPrice(item)
ProductService->>Inventory: checkStock()
Inventory-->>ProductService: quantity
ProductService-->>ShoppingCart: price + stock
end
ShoppingCart->>ShoppingCart: calculateTotal
sequenceDiagram
participant OrderService
participant Order
participant EmailService
OrderService->>Order: create new Order()
activate Order
OrderService->>Order: addItem(product)
OrderService->>Order: confirm()
Order->>EmailService: sendConfirmation()
EmailService-->>Order: done
OrderService->>Order: destroy
deactivate Order
destroy Order
create: Creates new object | destroy: Object destruction
validateUser(), calculateTotal())Create a sequence diagram for an online purchase with these objects:
Scenario:
| Diagram | Best For | Key Question |
|---|---|---|
| Use Case | Requirements gathering, stakeholder communication | What can users do? |
| Activity | Business processes, algorithms, workflows | What steps happen? |
| Sequence | Object interactions, API design, debugging | How do objects talk? |
| Class | System structure, database design | What entities exist? |
Design a Hotel Reservation System using all three diagram types:
| Diagram | Shows | Key Elements |
|---|---|---|
| Use Case | System functionality | Actors, use cases, include/extend |
| Activity | Process flow | Activities, decisions, fork/join, swimlanes |
| Sequence | Object interactions | Lifelines, messages, fragments (alt/loop/opt) |