Ejemplos de diagramas de secuencia UML
Tres flujos, cada uno necesita algo que los otros no: un inicio de sesión que se ramifica, un pago que reintenta y un evento al que nadie responde.
6 min de lecturaUML 2.5.17 de 28
01A login, where the branch is the point
Read the diagram above as two stories sharing a beginning. Everything above the alt frame happens either way; inside it, exactly one compartment runs, and the guards in square brackets say which. That is the thing a sequence diagram does that a numbered list of steps cannot, and it is why this flow is the standard first example.
The detail worth copying is in the failure branch. It does not simply return an error: it fires record(failure) at the attempt log first, drawn with an open arrowhead because nothing waits for it. A diagram whose failure path is one arrow labelled "error" is a diagram that has not thought about the failure path, and the throttling requirement everybody discovers three weeks later is exactly what lives in that branch.
02A payment, where the loop and the self-call are the point
Two things here do not appear on a first sequence diagram. The loop fragment's guard names both a bound and a condition - [3 times, while transient] - because a loop with no bound is a diagram promising an outage, and one with no condition does not say what stops it early.
The other is backoff(), a message from Checkout to itself, drawn as an arrow that leaves a lifeline and returns to it. Self-calls are worth drawing exactly when the delay or the decision is part of the story - here it is the whole reason the second attempt succeeds - and worth omitting when they are ordinary internal work.
Note also what is not here: no dashed reply after markPaid(orderId). The order store answers, of course, but the answer carries nothing this flow reasons about, and drawing every reply is how a diagram of six interesting messages becomes a diagram of twelve.
03An event, where nothing is answered
This is the example almost nobody draws, and the one most worth having. Every arrow is asynchronous - solid line, open head - and there is not a single dashed return. The order service publishes and moves on; the warehouse and the email service each receive the event, and nothing on this diagram says which of them finishes first, because nothing in the system does either.
Drawing this flow with filled arrowheads and replies would be a different system: one where publishing blocks until both consumers have run, which is precisely the property a broker exists to remove. The notation distinguishes them, and the distinction is worth more here than anywhere else in UML.
In one line each
- 01An alt fragment carries every outcome; if only one branch has a guard, you meant opt.
- 02Give a loop both a bound and a condition, and keep the failing reply inside it.
- 03Open arrowhead plus no reply means asynchronous. Do not draw a return that did not happen.
- 04Draw a self-call when the delay or decision is part of the story, not for ordinary internal work.
- 05Omit replies that carry nothing the flow reasons about.
For the method rather than the examples, read how to draw a sequence diagram; for the full notation, the sequence diagram article.
04Preguntas frecuentes
¿Cuál es un buen ejemplo de diagrama de secuencia UML?
El inicio de sesión es el ejemplo estándar porque se ramifica, y ramificar es precisamente lo que un diagrama de secuencia sabe hacer y una lista numerada no. Además merece la pena dibujar un reintento y una publicación asíncrona, porque juntos cubren las tres formas de las que se compone casi cualquier flujo real.
¿Cómo muestro un reintento en un diagrama de secuencia?
Con un fragmento loop alrededor de los mensajes que se repiten y una guarda que nombre el número y la condición, por ejemplo tres veces, mientras sea transitorio. Dibuja también la respuesta fallida dentro del bucle: un bucle que solo contiene la petición esconde el motivo del reintento.
¿Cómo se dibuja un mensaje asíncrono?
Como una línea sólida con punta abierta, en lugar de la punta rellena que lleva una llamada síncrona, y sin flecha de retorno después. La ausencia de respuesta es justo el punto: quien envió no esperó, y una flecha de retorno discontinua afirmaría lo contrario.
Lecturas relacionadas