Skip to main content

Activity Diagram

Editorial

Purpose of the Activity Diagram

The Class Diagram shows us what the system is made of. The Activity Diagram shows us how it behaves — the step-by-step workflow from a vehicle arriving at the entrance to leaving with a calculated fee.

Activity Diagrams are behavioral UML diagrams (covered in Fundamentals S5) that model workflows and decision points. They're especially useful for two things:

  1. Visualizing branching logic — what happens if no spot is available? What happens if the vehicle type doesn't match any spot?
  2. Verifying completeness — by tracing every path through the diagram, we can confirm our class design handles all scenarios

For the Parking Lot, we'll model two key workflows:

  • Park Vehicle — the happy path and the rejection path
  • Unpark Vehicle — ticket lookup, fee calculation, spot release

These workflows directly map to the parkVehicle() and unparkVehicle() methods in our ParkingLot class. The activity diagram helps us verify that our method design is complete before we write code.

Activity Diagram Elements

Before building our diagrams, let's review the key notation elements we'll use:

  • Initial Node (filled black circle) — where the workflow starts
  • Activity/Action (rounded rectangle) — a step in the workflow
  • Decision Node (diamond ◇) — a branching point with conditions on outgoing arrows
  • Merge Node (diamond ◇) — where branches come back together
  • Final Node (filled black circle inside a circle) — where the workflow ends
  • Flow Arrows — directed edges connecting activities, with guard conditions in [brackets] on decision branches

We covered these in detail in Fundamentals S5 (Activity Diagrams — Workflow and Decision Points). Here, we apply them to a real system.

Workflow 1: Park Vehicle

The Park Vehicle workflow begins when a driver arrives and ends with either a successful parking (ticket issued) or a rejection (lot full for that vehicle type).

Step-by-step trace:

  1. Vehicle Arrives — Driver enters with a vehicle (Car, Truck, or Motorcycle)
  2. Get Vehicle Type — System reads the vehicle type to determine spot compatibility
  3. Search Floors for Available Spot — System iterates through floors, calling floor.findAvailableSpot(vehicleType) on each
  4. Decision: Spot Found?
    • [Yes] → Proceed to park
    • [No] → Reject the vehicle (lot full for this type)
  5. Park Vehicle in Spot — Call spot.park(vehicle) to mark the spot as occupied
  6. Generate Parking Ticket — Create a ParkingTicket with vehicle, spot, and current timestamp
  7. Return Ticket to Driver — The driver receives the ticket

Key design insight: The decision at step 4 is critical. Notice that "lot full" is type-specific — there might be large spots available, but if the vehicle is a truck and all large spots are taken, the truck is rejected even though compact spots exist. This nuance is what makes the canFitVehicle(VehicleType) method on ParkingSpot essential.

Park Vehicle — Activity Diagram

A UML activity diagram of parking a vehicle, read left to right. From the start node: the vehicle arrives at the entrance; the system gets the vehicle type (vehicle.getType()); a merge node; it checks the next floor (floor.findAvailableSpot(type)); a decision asks whether a spot was found. If not, a second decision above it asks whether there are more floors: yes loops back to the merge to check the next floor; no rejects the vehicle — the lot is full for this type — and ends. If a spot was found, the flow goes right and down: park in the spot (spot.park(vehicle)), issue a parking ticket, kept in activeTickets, return the ticket to the driver, and end. A key shows the start, decision or merge and end symbols, and notes that "lot full" is per vehicle type: a truck is refused when every Large spot is taken, even with Compact spots free.

Workflow 2: Unpark Vehicle

The Unpark Vehicle workflow begins when a driver presents their ticket and ends with the fee being returned and the spot released.

Step-by-step trace:

  1. Driver Presents Ticket — Driver provides the ticket ID
  2. Lookup Ticket — System retrieves the ParkingTicket from activeTickets map
  3. Decision: Ticket Valid?
    • [No] → Return error (invalid ticket ID)
    • [Yes] → Proceed to unpark
  4. Remove Ticket — Remove the ticket from the activeTickets map
  5. Record Exit Time — System captures the current timestamp
  6. Calculate Duration — Compute time difference: exitTime - entryTime
  7. Calculate Fee — Call calculateFee(duration) on the active pricingStrategy
  8. Release Spot — Call spot.unpark() to mark the spot as available
  9. Return Fee to Driver — Display the calculated parking fee

Key design insight: Notice that the pricing strategy is invoked at step 7 — this is where the Strategy Pattern pays off. Whether the lot uses hourly pricing, flat-rate pricing, or any future pricing model, this step always calls the same calculateFee() method. The ParkingLot never knows which algorithm runs. This is polymorphism in action (Fundamentals S3).

Unpark Vehicle — Activity Diagram

A UML activity diagram of unparking a vehicle, read left to right along the top row and back along the bottom row. From the start node: the driver presents the ticket, with the ticket ID; the system looks the ticket up by ID in activeTickets; a decision asks whether the ticket is valid. If it is invalid, the flow goes up: return an error, Invalid ticket ID, and end. If it is valid, the flow goes right: remove the ticket from activeTickets, record the exit time (the current time), then down to the bottom row and back to the left: calculate the duration (exitTime - entryTime); calculate the fee via the PricingStrategy (pricingStrategy.calculateFee(duration)), with a note: Strategy pattern, the algorithm is swappable; release the parking spot (spot.unpark()); return the fee amount to the driver, the calculated parking fee; and end. A key shows the start, decision or merge and end symbols.

From Activity Diagrams to Code

Activity diagrams translate almost directly to method implementations. Each activity becomes a line of code, each decision becomes an if statement, and the flow order becomes the sequential execution order.

Here's how the Park Vehicle diagram maps to our ParkingLot.parkVehicle() method:

ActivityCode
Get Vehicle TypevehicleType = vehicle.getType()
Search Floorsfor floor in self.floors:
Find Available Spotspot = floor.findAvailableSpot(vehicleType)
Decision: Spot Found?if spot is not None:
[No] Lot Fullraise ValueError(...)
Park Vehiclespot.park(vehicle)
Generate Ticketticket = ParkingTicket(vehicle, spot)
Return Ticketreturn ticket

This direct mapping is why activity diagrams are valuable — they serve as pseudocode for your methods. In the Code Implementation tutorial, you'll see these exact steps translated into Python and Java.

The activity diagrams also reveal edge cases we'll address in the Testing tutorial:

  • What if two vehicles try to park in the last spot simultaneously?
  • What if a driver presents someone else's ticket?
  • What if the pricing strategy is null (not set)?