Skip to main content

Sequence Diagram — Split an Expense

Editorial

Purpose of This Diagram

The class diagram showed us what classes exist and how they relate structurally. The sequence diagram answers a different question: how do these classes interact at runtime to accomplish a specific task?

We'll trace the most important use case in Splitwise: adding an expense with a split. This flow touches almost every class in the system — from the service layer through strategy execution to balance updates — making it the perfect sequence to visualize.

By the end, you'll understand exactly which object calls which method, in what order, and what data flows between them. This is precisely what an interviewer expects when they say "walk me through the flow."

Identifying Participants (Lifelines)

A sequence diagram starts by identifying the objects involved in the interaction. From our class diagram, the "add expense" flow involves:

ParticipantRole in this Flow
ClientThe external caller (UI or API layer) that initiates the request
ExpenseServiceOrchestrates the entire workflow — splitting (the strategy validates the shares), creating the expense, balance updates, the group
SplitStrategyCalculates individual shares based on the chosen algorithm (equal/exact/percentage)
ExpenseCreated as the result — holds payer, amount, splits, and metadata
User (payer)The user who paid the expense — shown through its balance sheet, the object the service updates
BalanceSheetUpdated for each participant to reflect the new debt
GroupReceives the new expense in its expense list

Note that SplitStrategy represents whichever concrete strategy was chosen — the caller passes it in, and the service invokes it polymorphically. This is the Strategy Pattern (Fundamentals S12) in action.

Tracing the Message Flow

Let's trace the flow with a concrete example: Alice pays $120 for dinner, split equally among Alice, Bob, and Charlie.

Step 1 — Client initiates: The client calls ExpenseService.addExpense(group, payer=Alice, amount=120, description="Dinner", participants=[Alice, Bob, Charlie], strategy=EqualSplitStrategy, shares=[]).

Step 2 — Strategy calculates splits: ExpenseService delegates to SplitStrategy.split(120, [Alice, Bob, Charlie], []). The EqualSplitStrategy works in whole cents — to_cents(120) is 12,000 — and divides them by 3: 4,000 cents, $40 each. It returns [Split(Alice, 40), Split(Bob, 40), Split(Charlie, 40)].

Step 3 — Expense created: ExpenseService creates an Expense object with the payer (Alice), the amount ($120), the description ("Dinner"), the computed splits and the strategy.

Step 4 — Balance updates: For each participant who is NOT the payer, ExpenseService updates balances bidirectionally:

  • Alice's BalanceSheet: Bob owes Alice $40, Charlie owes Alice $40
  • Bob's BalanceSheet: Bob owes Alice $40
  • Charlie's BalanceSheet: Charlie owes Alice $40

Since Alice is both payer and participant, her own $40 share cancels out — she effectively paid $80 for others.

Step 5 — Group updated: The expense is added to the group's expense list, and ExpenseService also keeps it in its own list for expense history (R9).

Step 6 — Return: The created Expense is returned to the client.

Run the Sequence Diagram

Let's construct the sequence diagram showing the message flow in proper UML notation — with lifelines, activation bars, synchronous messages, and return values.

Run it below, message by message, as the code runs it: the client's addExpense, the strategy's split in whole cents, the Expense created, each debtor's balance sheet and then Alice's, the group, and the expense returned — with four predictions on the way. Three more runs use the same objects: the payer as the only participant, an expense without a group, and an amount finer than a cent.

Interactive: play the Splitwise "add expense" sequence diagram message by message — Alice pays $120 for dinner, split equally: addExpense, split in whole cents, the Expense created, four balance updates (each debtor's sheet, then Alice's), the group, the return — with each object's state over its head and four predictions; then the payer as the only participant (no balance changes), an expense without a group (the group never called) and an amount finer than a cent (refused inside split(), nothing created).

Complete Sequence Diagram

A UML sequence diagram of the Splitwise "add expense" flow, in the code's order, for the lesson's example: Alice pays $120 for dinner, split equally among Alice, Bob and Charlie. (1) The Client calls addExpense(group, Alice, 120, "Dinner", [Alice, Bob, Charlie], EqualSplitStrategy, []) on the ExpenseService. (2) The service calls split(120, [Alice, Bob, Charlie], []) on the EqualSplitStrategy, which works in whole cents (toCents: 12,000 / 3 = 4,000 each), creates Split(Alice, 40), Split(Bob, 40) and Split(Charlie, 40) and returns the splits. (3) The service creates the Expense with «create»(Alice, 120, "Dinner", splits, EqualSplitStrategy). (4) For each split except Alice's own (she paid), it updates the debtor's balance sheet first, then Alice's: updateBalance(alice.userId, 40) on Bob's, updateBalance(bob.userId, -40) on Alice's, updateBalance(alice.userId, 40) on Charlie's, updateBalance(charlie.userId, -40) on Alice's; a note: each sheet is keyed by the other user — Bob's holds Alice +40 (Bob owes Alice $40), Charlie's Alice +40, and Alice's holds Bob -40 and Charlie -40 (she is owed $80). (5) It calls addExpense(expense) on the Group, then keeps the expense in its own list for expense history (R9). (6) It returns the expense to the Client. A legend explains call, reply and create.

Key Design Decisions

1. Why does ExpenseService orchestrate (not Expense itself)?

The Expense class is a domain entity — it holds data but doesn't coordinate workflows. If Expense directly called BalanceSheet.updateBalance(), it would need references to all participants' balance sheets, violating the Single Responsibility Principle. ExpenseService acts as the coordinator, keeping Expense focused on being an expense.

2. Why bidirectional balance updates?

When Alice pays for Bob, we update BOTH Alice's balance sheet (Bob owes me $40) AND Bob's balance sheet (I owe Alice $40). This redundancy ensures either user can independently query their balances without traversing the other user's data. It's a deliberate denormalization for query efficiency.

3. Why does the client pass the strategy?

The split strategy is chosen by the user ("split equally" vs "split by exact amounts"). The client knows the user's choice, so it creates the appropriate strategy object and passes it in. The service invokes it polymorphically — this is the classic Strategy Pattern handoff seen in the Elevator System (DP8) and Ride-Sharing Service (DP22) designs.

4. What about the payer being a participant?

Alice paid $120 and owes $40 (her share). The net effect is Alice is owed $80 total. Our balance update loop only processes non-payer participants, so Alice's self-share is implicitly handled — she doesn't owe herself anything, and the other two participants' $40 debts sum to the $80 she's owed.