Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Communication patterns

Communication patterns describe how objects and components exchange information—synchronous/asynchronous messages, events, and higher-level patterns (observer, publish–subscribe, mediator, RPC, message queue)—and how to show them in UML sequence and communication diagrams.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Two or three sentences: Communication patterns are used by engineers when designing how parts of a software system interact: they decide whether calls are synchronous or asynchronous, which objects notify which, and how to draw those relationships in UML (sequence and communication diagrams) so developers and testers implement correct protocols and timing.

Key ideas

Start from first principles and define terms before use.

  • Object: an instance of a class that encapsulates state (data) and behaviour (methods). Use this term from class modelling.
  • Message: a request sent from one object to another to invoke behaviour. A message carries a name and optional parameters and may expect a return value.
  • Synchronous communication: a message where the sender waits (blocks) until the receiver finishes processing and returns a result. In designs, synchronous calls imply control transfer and tighter timing coupling.
  • Asynchronous communication: a message where the sender does not wait for the receiver to finish. The sender continues immediately; responses (if any) arrive later. Asynchronous messages reduce temporal coupling but introduce concurrency and ordering concerns.
  • Event / Signal: an occurrence that objects can react to; an event may be implemented as an asynchronous message or a publish/notify action. In UML, a Signal is a classifier that defines the payload of an asynchronous stimulus, and a Signal Event represents the receipt of a Signal.
  • Callback: a reference (pointer) to a function or method passed to another object so that the callee can call back (usually asynchronously) when work finishes.
  • Observer pattern: a design pattern where one object (the subject) maintains a list of dependents (observers) and notifies them automatically of state changes by calling their update method. This is a form of publish–subscribe where publisher knows about subscribers (push style).
  • Publish–subscribe (pub–sub): a decoupled pattern where publishers send messages to a broker or topic without knowing subscribers; subscribers register interest in topics and receive published messages. This is looser coupling than Observer and often implemented with message brokers or event buses.
  • Mediator pattern: a central object (the mediator) encapsulates interactions between other objects, reducing direct links between them. Use when many-to-many direct connections would cause high coupling.
  • Remote Procedure Call (RPC): synchronous (or sometimes asynchronous) request/response across process or network boundaries that looks like a local call to the programmer but involves marshalling and network transport.
  • Message Queue: an asynchronous transport where senders enqueue messages and receivers dequeue them; provides decoupling, buffering, and often reliability guarantees.

Exact statements and assumptions commonly used in exam answers

  • Definition (Synchronous): "A communication is synchronous iff the sender blocks until the receiver completes the requested action and returns. Assumption: both parties share a common time model where blocking is possible and desirable."
  • Definition (Asynchronous): "A communication is asynchronous iff the sender does not wait for the receiver; messages are delivered later or queued. Assumption: the system can handle concurrency and the ordering of messages may need explicit handling."
  • Rule (Coupling vs. Decoupling): "Direct method calls create tight coupling between caller and callee's interface; using a mediator or publish–subscribe lowers coupling at the cost of organizing infrastructure and possibly latency."
  • Rule (Ordering): "If message ordering matters, either use synchronous calls or provide sequence numbers or transactional guarantees in asynchronous channels. Assumption: the transport preserves FIFO ordering only if specified (e.g., single queue per producer–consumer)."

What to draw in exams (diagrams):

  • Sequence diagram (when asked): draw vertical lifelines for each object. Label each lifeline as objectName:ClassName (or :ClassName if the object name is omitted). Draw activation rectangles (thin stacked rectangles on lifelines) for the periods an object is active. Draw messages as horizontal arrows from sender activation to receiver activation. Label each arrow with the message name and parameters. Use a solid line with a filled arrow head for synchronous calls, a solid line with open arrow head for asynchronous messages (verify arrow style in your UML version), and a dashed return arrow for return values. Number messages if required. Show object creation with a create message to the object's lifeline start, and destruction with an X at the end of the lifeline.
  • Communication diagram (when asked): draw objects as labeled boxes or rectangles; draw links (lines) between objects that communicate; annotate each link with the message sequence number and message label like 1: placeOrder(item) or 1.1: confirm(). Use decimal numbering to show nested calls: 1, 1.1, 1.1.1, 2, etc. Show multiplicities on links only when multiple instances are relevant.

Relation to other topics: Communication patterns connect to Sequence models, Communication diagrams, State modelling (events and transitions), Design patterns (Observer, Mediator), and Component and deployment diagrams (for distributed messaging). Do not re-teach those topics; reference them by name.

Formulas

For communication patterns we list syntax and notations to remember (UML sequence and communication syntax): Caller -> Callee: operation(params) — synchronous call; arrow from caller to callee with a filled arrowhead. Remember: caller may be blocked until return. Caller --> Callee: message(params) — asynchronous message; arrow with open arrowhead (verify for your UML tool/version). Callee --> Caller: returnValue — dashed line for return message (label it with return value). In some notations return is omitted. objectName : ClassName — lifeline or instance notation. [condition] — guard for conditional messages, as in Caller -> Callee: op() [condition]. loop [i=1..n] — loop frame on a sequence diagram to indicate repetition (put the iteration constraint in square brackets). alt / opt / par — interaction operands in sequence diagrams for alternatives, optional and parallel fragments. 1, 1.1, 1.1.1 — hierarchical numbering scheme for communication-diagram messages to show nesting.

(Verify arrowhead styles with your UML version; see verify list.)

Worked examples

  1. Sequence diagram trace — ATM withdraw (step-by-step trace) Objects: User, ATM_UI:ATMInterface, Session:ATMSession, Bank:BankServer, Cash:CashDispenser. Steps and messages (show on a sequence diagram):
  1. User -> ATM_UI: enterCard(cardId) (user inserts card). // synchronous from UI point of view
  2. ATM_UI -> Session: startSession(cardId) // create session
  3. ATM_UI -> Bank: authenticate(cardId, pin) // synchronous remote call
  4. Bank -> ATM_UI: authResult(bool) // return
  5. User -> ATM_UI: requestWithdrawal(amount)
  6. ATM_UI -> Bank: checkFunds(session, amount)
  7. Bank -> ATM_UI: fundsOk(bool)
  8. ATM_UI -> Bank: debitAccount(session, amount)
  9. Bank -> Cash: releaseCash(amount) // asynchronous command to dispenser
  10. Cash -> ATM_UI: cashTaken() // event when user takes cash
  11. ATM_UI -> Bank: endSession(session) Final answer: Draw lifelines vertical for User (actor), ATM_UI, Session, Bank, Cash. Draw activation bars for ATM_UI, Bank and Cash during calls. Use synchronous arrows for steps 3,6,8; an asynchronous arrow for 9 if the dispenser acts independently. Label returns with dashed arrows (steps 4,7, and the return from 8). End with a return for step 11.
  1. Communication diagram trace — Publish–subscribe for sensor readings Objects: Sensor:TemperatureSensor, Broker:EventBroker, Display:TempDisplay, Logger:DataLogger. Design: Sensor publishes tempReading(value) to Broker asynchronously. Broker forwards to Display and Logger. Messages with numbering (communication diagram): 1: publish(tempReading(22.5)) 1.1: deliver(Display, tempReading(22.5)) 1.2: deliver(Logger, tempReading(22.5)) Describe what to draw: boxes for Sensor, Broker, Display, Logger. Draw links Sensor–Broker, Broker–Display, Broker–Logger. Annotate links with messages and numbering above. Explain ordering: If subscribers need ordered processing, Broker must preserve ordering or attach sequence numbers. If not, messages may be delivered in parallel.

  2. Theory/model-answer — "Compare Observer and Publish–Subscribe and give when to use each." (Exam-style long answer) Model answer should define both patterns, show their coupling differences, mention typical implementation (Observer: subject holds list of observers and calls update; Pub–sub: broker routes messages to topics), show deployment concerns (Observer usually in-process, pub–sub often distributed), and give examples: GUI listeners (Observer), distributed logging/metrics (pub–sub). End with trade-offs: decoupling vs latency, complexity of broker, reliability.

Common mistakes

  • Using communication diagram numbering incorrectly: forgetting hierarchical decimal scheme for nested calls.
  • Not stating assumptions: e.g., assuming FIFO order without specifying transport guarantees.
  • Mixing up Observer and Pub–Sub: forgetting that Observer usually implies direct references while Pub–Sub uses a broker.
  • Omitting return messages in sequence diagrams where a return value is part of the protocol (causes incomplete protocol description).

For your exam

Kinds of questions: definitions (define message, synchronous/asynchronous, event), compare/contrast (Observer vs Pub–Sub), draw diagrams (sequence and communication diagrams for small scenarios), design questions (choose pattern for given requirements), and trace problems (step-by-step message trace). What a full-marks answer includes:

  • Precise definitions of terms with bolded term names (as given above).
  • Assumptions stated (e.g., whether calls are local or remote, whether ordering is guaranteed).
  • Correct diagram drawn as described in Key ideas with labels, activation bars, message arrows, and numbering where required.
  • Explanation of trade-offs: coupling, latency, reliability, complexity.
  • For design questions: justify choice of pattern (why Observer/Mediator/Pub–Sub) and mention failure modes and how to handle them (timeouts, retries, queues).

Quick check

  1. What is the key difference between synchronous and asynchronous communication?
  2. In a sequence diagram, how do you show that an object is active while processing a message?
  3. Name one advantage of using a mediator pattern over many direct connections.

Answers: 1. Synchronous blocks the sender until the receiver finishes; asynchronous does not wait. 2. Draw an activation bar (narrow rectangle) on the object's lifeline. 3. It reduces coupling by centralising interaction logic and avoids many-to-many direct links.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?