In a UML state machine diagram, what does a transition out of a composite state do to its active substates?
answer
- A state with a machine inside it
- One arc on the boundary, many substates
- Exit runs innermost first, outward
- Cancellation drawn once instead of eleven times
- The innermost enabled transition wins
basics
~20 sIt exits them. Leaving a composite state first exits whichever substate is currently active, running that substate's exit action, then runs the composite's own exit action — so one arc drawn on the boundary covers every substate inside it.
solid answer
~40 sA **composite state** is a state that contains a nested region with its own initial pseudo-state and substates. An arc drawn from the composite's boundary applies to the composite as a whole, which means it can fire while *any* substate is active. When it does, exit actions run from the inside out: the active substate's first, then the composite's. That is the point of nesting — six substates that all handle the same cancellation event need one arc on the boundary instead of six near-identical arcs. Two rules matter when reading such a diagram. An arc may also target a substate directly, which skips the region's initial pseudo-state but still runs the composite's entry action first. And where a substate and its enclosing composite both handle the same event, the **innermost** enabled transition wins.
code
pseudocode · 9 linescomposite state IN_TRANSIT
entry / notifyShipper
exit / stopTracking
region:
(initial) -> AT_ORIGIN
AT_ORIGIN -> IN_LINEHAUL on departed
IN_LINEHAUL -> AT_DESTINATION on arrived
IN_TRANSIT -> CANCELLED on bookingCancelled // fires from any substatego deeper
Recognise a composite state as a state box with a smaller machine drawn inside it, and know that a single arrow on its boundary can apply to everything nested within.
Explain what a boundary transition means for the currently active substate, and give the exit order: innermost first, then outward through each enclosing state.
Be ready to resolve a conflict — a substate and its enclosing state both handling the same event — and to say why the inner one wins and when that override is worth having.
Own the structural judgement: when nesting genuinely removes duplicated arcs, and when it hides the lifecycle so that a flat machine or a second modelled object would read better.
## What a composite state is A **composite state** is a state that contains a machine of its own. Inside its boundary is a **region** holding substates, arcs between them, and an initial pseudo-state marking where the region starts. `In Transit` might contain `At Origin`, `In Linehaul` and `At Destination`. When the object is in a substate, it is also in the enclosing composite state — that is the whole idea, and it is why an arc drawn on the outer boundary is meaningful. Composite states exist to remove duplication and to let a reader work at one level at a time. A flat machine forces every reader to hold every state in mind at once; a nested one lets them read the outer lifecycle first and open a box when they need the detail. ## Entering and leaving The order of behaviour is strictly outside-in on the way in and inside-out on the way out. | Direction | Order | Note | | --- | --- | --- | | Entering the composite normally | composite entry action, then the region's initial pseudo-state fires, then the substate's entry action | the default path when the arc targets the composite itself | | Entering a substate directly | composite entry action, then that substate's entry action | the region's initial pseudo-state is skipped entirely | | Leaving from a substate | substate exit action, then composite exit action | true whether the arc starts at the substate or at the boundary | So an arc from the boundary of `In Transit` to `Cancelled` does not somehow bypass the inside. It exits whichever substate is active, running that substate's exit action, then runs the composite's exit action, and only then takes effect and enters `Cancelled`. ## Why the boundary arc is the point On a freight-booking portal, `In Transit` held eleven substates and every one of them had to accept a `bookingCancelled` event. Drawn flat, that is eleven arcs to the same target, all with the same label, and a reviewer must check each one. Drawn as a composite, it is a single arc on the boundary. When a twelfth substate is added, it inherits the cancellation path for free rather than depending on someone remembering to draw a twelfth arc — which, on a team whose on-call rotation was eating about half its capacity, is exactly the kind of detail that got missed twice before the model was nested. The practical checklist for deciding to nest: - Several states share the same outgoing arc to the same target on the same event. - Several states share the same entry or exit behaviour, which can move onto the composite. - The outer lifecycle is meaningful on its own to a reader who does not care about the detail. - The substates are genuinely phases of one condition, not unrelated states grouped for tidiness. ## Inner transitions win If a substate and its enclosing composite state both define a transition on the same event, the **innermost** enabled transition fires and the outer one does not. This is a feature: it lets a specific substate override the group behaviour — `In Linehaul` can handle `bookingCancelled` with its own arc that also releases the trailer — while every other substate falls through to the boundary arc. It is also a trap for the reader who assumes the outer arc always applies, so an override is worth a note on the diagram. ## Orthogonal regions A composite state may hold **more than one region**, drawn separated by a dashed line. The object is then in one substate of *each* region simultaneously. This models genuinely independent concerns of the same object: a booking's `Payment` region moving through `Unpaid` and `Settled` while its `Documents` region moves through `Missing`, `Submitted` and `Cleared`. The regions do not synchronise unless you make them, and an event is offered to every region. Use orthogonal regions only when the concerns really are independent. Two regions with three substates each replace nine combined states, which is a large win; but if every change in one region forces a change in the other, they were never independent and the flat nine states told the truth. Note also that this is concurrency *of one object's state*, not the concurrency notation used in a process diagram, and conflating the two is a common misreading. ## When nesting stops helping Nesting three or four levels deep buys less and less. Each level adds an entry and exit action a reader must trace, and a deeply nested machine is often a sign that two different objects have been modelled as one. If the substates of a substate have their own substates, ask whether the inner machine belongs to a separate object with a lifecycle of its own.
- What does it mean when a transition targets a substate inside a composite state directly?It enters the composite and lands on that substate, skipping the region's initial pseudo-state. The composite's entry action still runs first, then the substate's. It is useful for resuming a known point, but it couples the outside of the diagram to the composite's internal structure, so every change inside the region risks invalidating an arc drawn from outside it.
- A substate and its enclosing composite state both handle the same event. Which transition fires?The substate's. The innermost enabled transition takes priority, and the enclosing arc does not fire at all. That is what lets one substate override the group behaviour while the rest fall through to the boundary arc. It is also easy to misread, so an override worth having is worth calling out explicitly when you walk someone through the diagram.
saying these in an interview costs you the question
- Thinks a boundary arc only fires from the composite's first substate
- Runs the composite's exit action before the active substate's
- Assumes entering a composite always starts at its initial substate
- Draws the same cancellation arc from every substate and calls it explicit
- Believes an enclosing transition beats a substate's on the same event
- Treats orthogonal regions as a process diagram's concurrency notation