4.5.4 · D4Software Engineering

Exercises — UML — use case, class, sequence, activity, state machine, component diagrams

2,379 words11 min readBack to topic

The seven diagram types you must know (drawn once so we never rely on memory alone):

Figure — UML — use case, class, sequence, activity, state machine, component diagrams

Read the map top-down: the two grand families (Structural = static shape, Behavioural = what happens over time) split into the concrete diagrams. Keep this picture beside you — most exercises ask "which diagram?" and the answer always lives on this tree.


Level 1 — Recognition

Goal: name elements and match a diagram type to the question it answers.

L1.1

Problem. Match each question to its correct UML diagram: (a) "Who uses the system and what goals can they achieve?" (b) "What data and methods does a BankAccount carry, and what is it related to?" (c) "For one traffic light, what states exist and what events switch them?"

Recall Solution

(a) Use Case diagram — it lists actors (roles outside the system) and their goals as ovals. No implementation detail. (b) Class diagram — the three-compartment box (name / attributes / operations) plus relationship lines. (c) State Machine diagram — one object's life as a set of states and event-labelled transitions. Anchor: look at the tree in the figure. (a) and (c) sit under Behavioural; (b) sits under Structural.

L1.2

Problem. In a class diagram, name the relationship each arrowhead/diamond means: (i) hollow triangle ▷, (ii) filled diamond ◆, (iii) hollow diamond ◇, (iv) dashed arrow ⇢.

Recall Solution

(i) ▷ = Inheritance / Generalization ("is-a"). Dog ▷— Animal. (ii) ◆ = Composition ("owns-a", the part dies with the whole). House ◆— Room. (iii) ◇ = Aggregation ("has-a", part can live independently). Library ◇— Book. (iv) ⇢ = Dependency ("uses temporarily", e.g. a method parameter type).

L1.3

Problem. True or false: a dashed vertical line in a sequence diagram is called an activation bar.

Recall Solution

False. The dashed vertical line is the lifeline (it shows the object exists through time). The activation bar is the thin rectangle laid over the lifeline showing the moments the object is actively running a method. An object can exist (lifeline present) but be idle (no bar).


Level 2 — Application

Goal: apply the include/extend, multiplicity, and message rules correctly.

L2.1

Problem. An online shop: Checkout always performs Validate Cart, and Checkout optionally Apply Coupon only if the user has a code. Write both stereotype relationships with the correct arrow direction.

Recall Solution
  • Checkout --<<include>>--> Validate Cart. The <<include>> arrow points from the base to the included case, because the base always depends on it. (incluDe → Depends always.)
  • Apply Coupon --<<extend>>--> Checkout. The <<extend>> arrow points from the extension to the base, because the optional extra reaches into the base. (eXtend → eXtra/optional.) Notice the directions are opposite — that is the whole distinction.

L2.2

Problem. An Order must contain at least one OrderLine; an OrderLine belongs to exactly one Order, and if the Order is deleted its lines are meaningless. Write the relationship with multiplicities and the correct diamond.

Recall Solution

In words: Order "1" ◆—— "1..*" OrderLine.

  • Filled diamond ◆ on the Order side → composition, because deleting the Order destroys its lines (lifecycle test: must the part die? → yes).
  • 1..* means "one or more" (at least one line). 1 on the Order side means each line has exactly one owning order.

L2.3

Problem. In a sequence diagram, an ATM calls validate(pin) on the Bank and waits for the answer. Which arrow type, and what draws the reply?

Recall Solution
  • The call is a synchronous message: a solid line with a filled (solid) arrowhead — the caller blocks until it returns.
  • The reply is a return message: a dashed line with an open arrowhead (⇠) carrying the result, e.g. ok. (If the ATM did not wait, it would be an asynchronous message — solid line, open arrowhead.)

Level 3 — Analysis

Goal: diagnose a modelling choice and defend the right one.

L3.1

Problem. A student modelled University ◆—— Student (composition). Critique this. Should it be composition or aggregation?

Recall Solution

It should be aggregation ◇, not composition ◆. Apply the lifecycle test: "If I delete the University, must every Student cease to exist?" No — students transfer, graduate elsewhere, keep existing as people. The part outlives the whole → aggregation (hollow diamond). Composition is reserved for parts with no independent meaning once the whole is gone (e.g. Invoice ◆—— InvoiceLine).

L3.2

Problem. A workflow: after Payment Received, the system must both Send Confirmation Email and Update Inventory, and only then Close Order. A student drew a decision node (◇) splitting into the two tasks. What is wrong, and what is correct?

Recall Solution

A decision (◇) chooses exactly one guarded branch — mutually exclusive. But here we need both tasks to run, so a decision is semantically wrong. Correct: use a fork (━) to launch both actions in parallel, then a join (━) to wait until both complete before Close Order. See the figure below: fork/join (parallel) vs decision/merge (choose one).

Figure — UML — use case, class, sequence, activity, state machine, component diagrams

L3.3

Problem. You need to show which object sends which message, in what order, during a single login attempt. A teammate suggests an activity diagram. Is that the best choice? Justify.

Recall Solution

No — a state machine is also wrong here. The right tool is a sequence diagram. Reason: the requirement is object A talks to object B, message by message, in time order. A sequence diagram puts objects across the X-axis and time down the Y-axis — exactly this need. An activity diagram hides objects and shows only the flow of work, so it cannot show "who talked to whom". (Compare: use activity when you care about the workflow, not the collaborators.)


Level 4 — Synthesis

Goal: design a diagram from a plain-English spec.

L4.1

Problem. Design a state machine for a document with states Draft, Under Review, Published, Archived. Events: submit (Draft→Under Review), approve (Under Review→Published), reject (Under Review→Draft), archive (Published→Archived). Also: while Under Review, an editor may comment without changing state. Write all states, the initial pseudostate, and every transition in event [guard] / action form.

Recall Solution
  • Initial ● → Draft.
  • DraftsubmitUnder Review
  • Under ReviewapprovePublished
  • Under ReviewrejectDraft
  • Under ReviewcommentUnder Reviewself-transition (fires, records a comment, state unchanged)
  • PublishedarchiveArchived
  • ArchivedFinal ◉ (nothing leaves Archived).

The comment self-transition is the key modelling choice: an event that does something (records the comment as an /action) yet does not move the object to a new state.

Figure — UML — use case, class, sequence, activity, state machine, component diagrams

L4.2

Problem. Design a use case slice for a library kiosk. Actor Member can Borrow Book and Return Book. Borrowing always requires Authenticate Member. Borrowing optionally triggers Send Reminder if the book is due soon. A Librarian actor can also Return Book (a specialization of Member's ability is not needed — just shared). List actors, use cases, and the include/extend/association relations.

Recall Solution
  • Actors: Member, Librarian.
  • Associations: Member —— Borrow Book, Member —— Return Book, Librarian —— Return Book.
  • Include (always): Borrow Book --<<include>>--> Authenticate Member (base depends on it every time).
  • Extend (optional): Send Reminder --<<extend>>--> Borrow Book (only when due-soon condition holds; arrow points into the base from the extension).
  • Authenticate Member is factored out via <<include>> because it is shared and never skipped — the textbook reason to include.

Level 5 — Mastery

Goal: integrate multiple diagram types on one system consistently.

L5.1

Problem. An ATM cash-withdrawal. You must produce three consistent views:

  1. one use case relation showing that withdrawing always authenticates;
  2. one sequence fragment showing the synchronous validate call with an alt on balance;
  3. one state machine for the ATM object with states Idle, Authenticating, Dispensing. Ensure the same "authenticate" step appears in all three, consistently.
Recall Solution

(1) Use case: Withdraw Cash --<<include>>--> Authenticate — authentication is mandatory, factored out.

(2) Sequence (single scenario):

  • Customer → ATM : insertCard()
  • ATM → Bank : validate(pin) (synchronous — solid filled arrowhead, ATM waits)
  • Bank ⇠ ATM : ok (dashed return)
  • alt [balance ≥ amount]ATM → Bank : debit(amount) ; ATM → Customer : dispenseCash()
  • else [balance < amount]ATM → Customer : showError()

(3) State machine:

  • ● → Idle
  • IdleinsertCard / readCardAuthenticating
  • Authenticatingvalidate [ok]Dispensing
  • Authenticatingvalidate [not ok] / ejectCardIdle
  • DispensingcashTakenIdle

Consistency check: the validate/authenticate step is present in all three — as an <<include>> (use case), as a synchronous message (sequence), and as the Idle → Authenticating → Dispensing transitions (state machine). The alt [balance ≥ amount] in the sequence corresponds to the two guarded outcomes; the [not ok] transition matches the else error path. Different viewpoints, one system.

L5.2

Problem (counting / analysis). In the L5.1 state machine, how many distinct transitions (arrows, including the initial-pseudostate arrow) are drawn? And how many of them are guarded (have a [condition])?

Recall Solution

Count the arrows:

  1. ● → Idle (initial)
  2. IdleAuthenticating
  3. AuthenticatingDispensing [ok]
  4. AuthenticatingIdle [not ok]
  5. DispensingIdle

Total transitions = 5. Guarded transitions = 2 (arrows 3 and 4 carry [ok] / [not ok]).


Recall Self-test summary (reveal after finishing)

Which family is a Class diagram in? ::: Structural (static shape). Direction of the <<include>> arrow? ::: From the base use case to the included one. Lifecycle test that separates composition from aggregation? ::: "If I delete the whole, must the part die?" Yes → composition ◆; No → aggregation ◇. Decision ◇ vs Fork ━? ::: Decision picks exactly one guarded branch; fork activates all branches in parallel. What is an activation bar (not the lifeline)? ::: The rectangle showing when an object is actively running a method.

See also: Design Patterns · Software Architecture · SOLID Principles · Flowcharts · Finite State Machines