Skip to content
Tutorials Center
My learning

Topics

Lesson 38 of 39 ยท Projects and Practice

UML Practice Exercises

Twelve UML exercises on state machines, components, deployment, packages, OCL and code mapping, each with a solution sketch and runnable checkers.

UML is a skill you only learn by drawing. These exercises train what chapters 5 to 8 taught: state machines, components, deployment, packages, constraints and the link from model to code. Do each one on paper or in a text file before opening the hint. Where the exercise can be checked by a program, a runnable checker follows.

Draw with Mermaid so you can paste your work into any page that renders it, or use any tool you like. Do not worry about perfect notation; the aim is precise thinking.

State machines

1. Traffic light. Model a pedestrian crossing: the light is green for cars by default. When a pedestrian presses the button, wait 5 seconds, show amber for 3 seconds, red for 15 seconds, then return to green. A second press during the red phase is ignored. Draw the state machine with after(...) events.

2. Vending machine. States: Idle, HasMoney, Dispensing, OutOfStock. Events: insertCoin, select, refund, restock. Add guards for insufficient money and out-of-stock items. List every (state, event) pair that is ignored.

3. Media player with hierarchy. Use a composite state On with substates Stopped, Playing, Paused, and an orthogonal region Volume with Muted and Normal. Add a history state so that switching the player off and on resumes where it was.

Hint for 2: a flat table of 4 states by 4 events has 16 cells; at least half should be intentional "ignored" cells.

Checker for exercise 2

Write your transitions as (state, event) -> state and see what the program says is missing or unreachable.

Python Check a vending machine table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output reveals that OutOfStock is unreachable: nothing leads into it. Which arrow is missing? (A select with an empty slot, guarded by [stock = 0].)

Components and deployment

4. Plugin system. A text editor core loads plugins. Draw the components: Core, SpellChecker, GitIntegration, and the interfaces that make them plug in (IPlugin, IEditorApi). Who requires, who provides? Mark which arrows must point toward Core.

5. Three-tier deployment. A mobile app talks to an API behind a load balancer, which uses a PostgreSQL database and a Redis cache. Draw the deployment diagram with protocols and two API replicas. Mark single points of failure.

6. Containerize. Convert exercise 5 into Docker/Kubernetes terms: which node types become execution environments, which artifacts are images?

Checker for exercise 5

Python Find single points of failure in your topology

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

You should get lb. What change makes the list empty? (A second load balancer, and then the database replica question comes next.)

Packages and layers

7. Layer check. Given packages web, application, domain, infrastructure, and these dependencies: web -> application, application -> domain, infrastructure -> domain, domain -> infrastructure, web -> infrastructure. Which dependency breaks the architecture rules? Fix it with an interface.

8. Instability. Compute Ca, Ce and I for each package in exercise 7, then say which package is the most stable.

Checker for exercises 7 and 8

Python Coupling metrics and rule violations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Constraints

9. OCL for a bank. Write invariants and contracts: an account balance is never below the overdraft limit; transfer(a, b, amount) requires amount > 0, a <> b and enough funds, and guarantees that the sum of both balances is unchanged.

10. OCL for a course. A Course has Students, with a maximum of 30 per course. A student may enroll in at most 5 courses at a time. Write the invariants using size() and forAll.

Checker for exercise 9

Python Check the transfer contract

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

From model to code

11. Map the diagram. Given Order (composition, one to many) OrderLine, OrderLine (association, many to one) Product, and Product with an abstract price(), Book and Course as subclasses. Write the Python skeleton. Which relationship decides where the list of lines is created?

12. Sequence to test. Take the sequence diagram "Register user": UserService calls UserRepository.exists(email), then PasswordHasher.hash(pw), then UserRepository.save(user) and Mailer.sendWelcome(user). Write two tests: one for a new email, one for an existing email. What must the mailer not receive in the second test?

Solution sketches

  1. States Green, WaitingPress (after 5 s), Amber (after 3 s), Red (after 15 s). The press event is ignored in Amber and Red; there is no arrow for it.
  2. Put [stock = 0] and [money < price] guards on select, and an OutOfStock state with restock leading out.
  3. Volume region with Normal and Muted, toggleMute between them, and an H history in On.
  4. Core provides IEditorApi and requires IPlugin; plugins provide IPlugin and require IEditorApi. Arrows point from plugins to Core's interface, never from Core to a concrete plugin.
  5. domain -> infrastructure violates the layering. Introduce OrderRepository as an interface in domain, implemented in infrastructure.
  6. inv: self.balance >= -self.overdraft; for transfer, post: a.balance + b.balance = a.balance@pre + b.balance@pre.
  7. Composition means Order creates its OrderLines (in add_line) and exposes no way to build them elsewhere.
  8. The second test must not call hash, save or sendWelcome: only exists is sent and an error is returned.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In exercise 2, which fix makes OutOfStock reachable?

    A state needs at least one incoming transition from the reachable part of the machine.
  2. 2. Which package dependency violates the layer rules in exercise 7?

    The domain must not depend on infrastructure; define an interface in domain instead.
  3. 3. What guarantees that a transfer neither creates nor destroys money?

    The postcondition compares the total after the call with the total before.
  4. 4. Which part of a sequence diagram tells you which tests to write?

    Every branch of a fragment is a path that needs a test.

Summary

  • Practice by drawing: state machines, components, deployment, packages, constraints and code mapping.
  • Use small checkers to find unreachable states, single points of failure and layer violations.
  • Contracts and invariants turn into assertions; fragments turn into tests.
  • Compare your solution with the sketches only after your own attempt.
  • Keep your diagrams as text, so you can review and improve them over time.

Next: UML Interview Questions