Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 50 ยท Modeling with UML

State, Component and Deployment Diagrams

Learn UML state machine diagrams (states, transitions, guards), component diagrams (interfaces) and deployment diagrams (nodes), with runnable stand-ins.

Three more UML diagrams complete the everyday toolkit. A state machine diagram describes the life of one object: the states it can be in and what causes it to move between them. A component diagram shows the replaceable building blocks of a system and the interfaces that connect them. A deployment diagram shows where those pieces actually run: which software on which machine.

None of these can run on the site; UML is a notation, so the diagrams are read-only pictures. The runnable examples are JavaScript stand-ins: a small state machine driven by a transition table, and a checker that matches the interfaces components provide with those they require. They model the ideas; they are not UML tools or a real deployment system.

State machine diagrams

Many things in software have a life cycle that depends on history: an order is draft, then paid, then shipped; a connection is closed, opening, open; a ticket is new, assigned, resolved. A state machine captures this. It has four ingredients:

  • States: conditions an object stays in for a while, drawn as rounded boxes ("Paid", "Shipped").
  • Transitions: arrows from one state to another. A label has the form event [guard] / action.
  • Events: things that happen and may trigger a transition ("pay", "cancel").
  • Start and end: a filled circle marks the initial state; a bullseye marks a final state.

A guard is a condition in square brackets. The transition happens only if the event occurs and the guard is true. An action after a slash is work done during the transition ("send receipt"). States can also have entry and exit actions that run whenever the state is entered or left.

stateDiagram-v2
    [*] --> Draft
    Draft --> Submitted : submit [cart not empty]
    Submitted --> Paid : payment received
    Submitted --> Cancelled : cancel
    Paid --> Shipped : ship
    Paid --> Refunded : refund
    Shipped --> Delivered : deliver
    Delivered --> [*]
    Cancelled --> [*]
    Refunded --> [*]

An order's life cycle. Only the listed transitions are allowed: a Shipped order cannot be cancelled.

The value of the diagram is in what it forbids. Reading it you immediately see which things cannot happen (you cannot ship an order that has not been paid), and each missing arrow is a question: "what should happen if a customer cancels after shipping?" Such questions are cheap to answer in a diagram and expensive to discover in production. This is also why state diagrams are a good source of test cases: every transition, and every forbidden event in each state, is a test.

Some advanced notation is worth knowing: composite states group substates (a "Paid" state containing "Packing" and "Awaiting pickup"), history pseudo-states remember where to resume, and concurrent regions model independent parts. For most work, simple states and transitions are enough. Do not draw a state machine for an object that has no interesting life cycle; a class that simply stores data does not need one.

A state machine in code

The standard way to turn a diagram into code is a transition table: for each state, which events are allowed, where they lead, and under which guard. The machine below implements the order life cycle. Run it to see allowed and forbidden events.

JS An order state machine

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Look at the three kinds of line. The empty order is blocked by the guard on submit. ship is ignored while the order is only Submitted, because the table has no such arrow there. cancel after shipping is ignored for the same reason. The table is data, so it can be reviewed against the diagram line by line, and a tool could even draw the diagram from it. A class that wraps the machine in methods (order.pay()) is the usual next step; see the state pattern in object-oriented design for the full version.

Component diagrams

A component is a modular, replaceable part of a system with a clear interface: a payment module, a user service, a library for sending email. A component diagram shows components and how they depend on each other through interfaces.

  • A provided interface (the "lollipop", a small circle on a stick) lists what the component offers to others.
  • A required interface (the "socket", a half circle) lists what it needs from others.
  • A connector joins a socket to a lollipop: this component uses that one's service.
  • Ports are named connection points; dependencies are dashed arrows when a full interface is not worth drawing.

The key idea is that components talk through interfaces, not through each other's insides. That is information hiding at the architecture level: a component can be rewritten or replaced as long as it still provides the same interface. A component diagram makes dependencies visible, so you can see which parts are tangled (many arrows) and which are independent. See architecture styles for common ways to arrange components.

Deployment diagrams

A deployment diagram answers "what runs where?". Its elements:

  • Nodes: boxes (often drawn as 3D boxes) for hardware or execution environments: a server, a phone, a container, a database host.
  • Artifacts: the files that get deployed, such as a .jar, an executable, a container image, a configuration file.
  • Communication paths: lines between nodes labeled with the protocol (HTTPS, TCP, a message queue).

Nodes can be nested: an application server node contains a runtime node that contains your artifact. The diagram helps with security reviews (which nodes are exposed to the internet?), capacity planning (how many instances?) and operations (what must be backed up?). Deployment diagrams also reveal failure points: a single database node connected to everything is a single point of failure.

flowchart LR
    subgraph phone["Customer phone"]
        app["Mobile app"]
    end
    subgraph web["Web server node"]
        api["Order service"]
        pay["Payment adapter"]
    end
    subgraph dbnode["Database node"]
        db[("Orders database")]
    end
    subgraph ext["Payment provider"]
        gateway["Payment gateway"]
    end
    app -- "HTTPS" --> api
    api --> pay
    api -- "SQL over TCP" --> db
    pay -- "HTTPS" --> gateway

Components placed on nodes. Arrows between boxes in different nodes are network calls, which can be slow and can fail.

This diagram combines both views: the boxes inside each node are components, and the nodes are machines. Arrows that cross a node boundary matter for design: each is a network call that needs timeouts, retries and security. Arrows inside a node are ordinary method calls.

A stand-in that checks interfaces and nodes

The stand-in below describes components with the interfaces they provide and require, plus the node where each runs. It checks that every required interface has a provider (a missing one is a broken diagram) and lists which dependencies cross nodes (the network calls).

JS Check required interfaces and find network calls

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The output reports one problem: PaymentAdapter requires CardGateway, but no component in the list provides it. In the drawing above, the gateway belongs to an external provider, so the model is simply missing that component; adding it with provides: ["CardGateway"] on a provider node would turn that line into one more network call. This kind of check is exactly why keeping the model as data, not just as a picture, pays off. The NETWORK lines show the arrows to plan for in terms of latency and failure handling.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a transition label 'submit [cart not empty]', what is '[cart not empty]'?

    Square brackets hold a guard: the transition fires only when the event occurs and the guard is true.
  2. 2. What is a good use of a state machine diagram?

    State machines show states and allowed transitions for one object, such as an order or a connection.
  3. 3. In a component diagram, how should components usually communicate?

    Interfaces hide the inside of a component, so each part can be replaced as long as the interface stays the same.
  4. 4. What does a deployment diagram show?

    Deployment diagrams map software artifacts onto hardware or execution nodes and the communication paths between them.

Summary

  • A state machine diagram shows an object's states, the events that move it between them, and guards and actions on transitions.
  • The missing arrows in a state diagram are as informative as the present ones; each is a test case or a design question.
  • A transition table is the usual way to turn a state machine into code.
  • Component diagrams show replaceable parts connected through provided and required interfaces.
  • Deployment diagrams place artifacts on nodes; connections between nodes are network calls that need timeouts and security.

Next: Data Flow and Entity-Relationship Diagrams.