Skip to main content

Sequence Diagram — Book a Ticket

Editorial

Purpose of the Sequence Diagram

The class diagram shows us what classes exist and how they relate. The sequence diagram shows us how those classes interact at runtime for a specific use case.

We're modeling the Book a Ticket Flow — the most critical workflow in the system. This is the flow an interviewer will ask you to walk through first: "Show me what happens when a user books a movie ticket."

The sequence diagram answers:

  • Which objects participate in the booking?
  • In what order do messages flow between them?
  • Where does the temporary seat hold happen?
  • What happens on payment success vs. failure?
  • When does the booking state transition from PENDING to CONFIRMED?

This diagram traces requirements R6 (seat hold), R7 (booking creation), R8 (payment), R10 (state lifecycle), and R11 (pricing strategy). Unlike simpler systems where "create booking" is a single step, our flow has a two-phase structure: hold (optimistic reservation) followed by confirm (on payment success). This two-phase design is what prevents double-booking.

Identifying Participants (Lifelines)

The booking flow involves eight objects as participants:

ParticipantClassRole in This Flow
:UserUserInitiates the booking request
:BookingServiceBookingServiceOrchestrates the entire booking workflow
:ShowShowProvides the ShowSeat instances for the requested show
:ShowSeatShowSeatTracks per-seat status (AVAILABLE → HELD → BOOKED)
:PricingStrategyPricingStrategyComputes the ticket price for each seat
:BookingBookingCreated during the flow — represents the reservation
:PaymentPaymentCreated during payment — records the transaction
:NotificationServiceNotificationServiceSends confirmation notification

Notice that Booking and Payment are created during this flow (they don't exist beforehand), so they appear as new lifelines partway through the sequence.

Tracing the Message Flow

Let's trace the booking step by step. The flow has two phases: Hold Phase (seat reservation) and Payment Phase (confirmation).

Phase 1: Hold Seats (Seat Selection)

  1. User --> BookingService: holdSeats(user, show, seatIds) — User selects seats and triggers the booking.
  2. BookingService --> Show: getShowSeats(seatIds) — Fetch the ShowSeat objects for the selected seat IDs.
  3. Show --> BookingService: returns List<ShowSeat> — The show returns the ShowSeat instances.
  4. BookingService --> ShowSeat: isAvailable() [for each seat] — Check that every selected seat is still available. If ANY seat is not available, the hold fails immediately.
  5. BookingService --> Booking: <<create>> Booking(user, show, showSeats, PENDING) — Create a new Booking in PENDING status.
  6. BookingService --> ShowSeat: hold(booking) [for each seat] — Mark each ShowSeat as HELD with the current timestamp and a reference to the booking. No other user can select these seats now.
  7. BookingService --> PricingStrategy: calculatePrice(show, seat) [for each seat] — Compute the price for each seat based on seat type, showtime, and day.
  8. BookingService --> Booking: setTotalAmount(totalPrice) — Set the computed total on the booking.
  9. BookingService --> User: returns Booking (PENDING) — Return the pending booking with the total amount. The user now has the hold period to complete payment.

Phase 2: Make Payment (Confirmation)

  1. User --> BookingService: makePayment(booking, paymentDetails) — User submits payment details.
  2. BookingService --> BookingService: validateHoldNotExpired(booking) — Self-call: check that the hold hasn't expired. If expired, reject payment and transition booking to EXPIRED.
  3. BookingService --> Payment: <<create>> Payment(amount, method, PENDING) — Create a Payment record.
  4. BookingService --> Payment: processPayment() — Process the payment (abstracted gateway call).
  5. Payment --> BookingService: returns PaymentStatus.COMPLETED — Payment succeeded.
  6. BookingService --> Booking: confirm() — Transition booking from PENDING to CONFIRMED.
  7. BookingService --> ShowSeat: book() [for each seat] — Transition each ShowSeat from HELD to BOOKED. These seats are now permanently reserved.
  8. BookingService --> NotificationService: notify(user, "Booking confirmed") — Send confirmation notification.
  9. BookingService --> User: returns Booking (CONFIRMED) — User receives the confirmed booking.

Failure Scenarios:

  • Step 4 fails (seat unavailable): BookingService returns error immediately — "One or more seats are no longer available." No booking is created.
  • Step 11 fails (hold expired): BookingService transitions booking to EXPIRED, releases all ShowSeats, returns error — "Hold expired. Please select seats again."
  • Step 13 fails (payment failed): BookingService transitions booking to CANCELLED, releases all ShowSeats via showSeat.release(), returns error — "Payment failed."

Timeout Scenario (System Timer):

If the user never calls makePayment within the hold period:

  • System Timer calls BookingService.expireHold(booking)
  • BookingService transitions booking to EXPIRED
  • BookingService calls showSeat.release() for each seat
  • Seats become AVAILABLE again for other users

Build the Sequence Diagram

Build it below one message at a time — lifelines, activation bars and message arrows — and watch the two-phase structure: hold phase (steps 1-9), then payment phase (steps 10-18). Predict the key calls before they are drawn, and watch each object's state change as its message arrives. Then play the three failure scenarios and the timeout.

Interactive: the Book a Ticket sequence diagram, built message by message. Eight lifelines — User, BookingService, Show, ShowSeat, PricingStrategy, Booking, Payment and NotificationService — exchange the 18 steps in two phases: the hold phase (holdSeats, getShowSeats, isAvailable for each seat, «create» Booking in PENDING, hold for each seat, calculatePrice for each seat, setTotalAmount, and the PENDING booking returned) and the payment phase (makePayment, validateHoldNotExpired, «create» Payment, processPayment returning COMPLETED, confirm, book for each seat, notify, and the CONFIRMED booking returned). Booking and Payment appear where they are created, activation bars show who is working, and each object's state shows over its head as messages arrive. Four checkpoints ask the learner to predict the next call. The three failure scenarios — seat unavailable at step 4, hold expired at step 11, payment failed at step 13 — and the System Timer's timeout replay with their release() loops and error replies.

Complete Sequence Diagram

Complete UML sequence diagram showing the Book a Ticket flow with User, BookingService, Show, ShowSeat, PricingStrategy, Booking, Payment, and NotificationService lifelines

Key Design Decisions

1. Why two phases (Hold + Pay) instead of a single atomic "book" operation?

In a single-phase design, the user selects seats and pays in one call. But payment takes variable time — the user enters card details, the gateway processes the charge, 3D Secure might require OTP verification. During this entire period (potentially minutes), the seats must be reserved for this user but not yet permanently booked. The two-phase approach — hold first, pay second — solves this by creating a PENDING booking that temporarily locks the seats. This is the standard approach used by BookMyShow, Fandango, and airline booking systems.

2. Why does BookingService orchestrate (not User calling classes directly)?

The User could theoretically call show.getShowSeats(), then showSeat.hold(), then pricingStrategy.calculate() directly. But this exposes the internal subsystem to the client and scatters booking logic across callers. BookingService acts as a Facade — one method call (holdSeats) handles availability check, booking creation, seat holding, and price calculation atomically. This is the same pattern used in the Library System (DP9) where Library orchestrates the checkout flow.

3. Why validate hold expiration in makePayment (step 11)?

Between holdSeats (step 1) and makePayment (step 10), time passes. The user might have spent 9 minutes entering payment details on a page while the hold was configured for 8 minutes. If we don't check expiration before processing payment, we could confirm a booking whose seats have already been released to another user — a double-booking. The explicit validateHoldNotExpired check is the guard against this race.

4. Why does ShowSeat.hold() take a Booking reference?

When the System Timer scans for expired holds, it needs to know which booking each held seat belongs to. The heldByBooking reference on ShowSeat enables the timer to expire the correct booking. Without it, the timer would have to search all bookings for each held seat — an O(n) scan vs. O(1) lookup.

5. Why loop fragments instead of a single bulk message?

We show hold(booking) and book() in loop fragments because each ShowSeat is an independent object with its own state machine. In the code, this is indeed a loop. Showing it as a loop fragment in the sequence diagram makes the per-seat state transitions explicit — if one seat fails to hold, the entire booking must roll back.