Skip to main content

Map sequence diagram to pseudo code

Short answer

Each lifeline in the sequence diagram becomes a class; each message becomes a method in the receiver. Synchronous calls wait for returns, asynchronous calls (like triggerBill) run non-blocking. The pseudocode shows OrderTaker calling CreditCardAgent synchronously, then OrderFulfillment, which asynchronously triggers BillingAgent.

Present the mapping process for the fig.1 model (the online order processing sequence diagram: OrderTaker, CreditCardAgent, OrderFulfillment, BillingAgent) using object-oriented based pseudo codes for capturing all important aspects of the model diagram.

The sequence diagram has the actor and the objects :OrderTaker, :CreditCardAgent, :OrderFulfillment and :BillingAgent, with the messages submitOrder, processCard, placeOrder, triggerBill (asynchronous) and acknowledgeOrder (return), and a "Credit Failure" variation.

Mapping process

  1. Each lifeline (object) becomes a class: OrderTaker, CreditCardAgent, OrderFulfillment, BillingAgent.
  2. Each message becomes a method in the receiver class, and the sender calls it.
  3. The order of messages (top to bottom) becomes the order of statements in the sender's method.
  4. A synchronous message becomes a normal call that waits for a result; an asynchronous message becomes a non-blocking call (a new thread or a queued message).
  5. The return message becomes the return value of the method.
  6. The variation (credit failure) becomes an if/else (or an exception) in the sender.
  7. Links between objects become references (fields) that are set up in the constructor.

Pseudocode

class OrderTaker
    attributes: creditCardAgent, orderFulfillment
    constructor(cca, of)
        creditCardAgent = cca
        orderFulfillment = of
    end constructor
    method submitOrder(order) : Acknowledgement
        approved = creditCardAgent.processCard(order.card, order.total)
        if approved is false then
            return Acknowledgement("Credit failure")   // variation
        end if
        orderFulfillment.placeOrder(order)
        return Acknowledgement("Order accepted")       // acknowledgeOrder
    end method
end class

class CreditCardAgent
    method processCard(card, amount) : boolean
        return bank.authorize(card, amount)            // external bank service
    end method
end class

class OrderFulfillment
    attributes: billingAgent
    constructor(ba)
        billingAgent = ba
    end constructor
    method placeOrder(order)
        reserveStock(order)
        schedulePacking(order)
        billingAgent.triggerBill(order)                // async, no wait
    end method
end class

class BillingAgent
    method triggerBill(order)                          // runs in own thread/queue
        invoice = createInvoice(order)
        send(invoice)
    end method
end class

// Actor / main
billing = new BillingAgent()
fulfil  = new OrderFulfillment(billing)
taker   = new OrderTaker(new CreditCardAgent(), fulfil)
ack = taker.submitOrder(order)
display ack

Important aspects captured: the ordering of calls, the responsibilities of each object, the synchronous wait for card approval before the order is placed, the asynchronous billing trigger that does not delay the customer's acknowledgement, and the alternative path for credit failure.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Object Oriented Analysis and Design chapter 4 questions →