Reading Time: six min

Land diagrams have been used in programming to draw the various possible states that a system can be in for decades. They too apace show which states lead to each other, and what triggers a change of country.

Of class, you tin can use these diagrams for anything system or process that has various states – library books, a publishing procedure, even punishment and reward states for your kids. They are very similar to period diagrams, simply UML has a notation specifically for state diagrams, which they also call state machines or statecharts.

State diagrams in UML note

The biggest departure betwixt country diagrams and activity or interaction overview diagrams is that state diagrams do non (necessarily) show the sequence. They just show what states are possible and their entry and exit points, non when those states are entered.

State diagrams can be very circuitous – you can nest state diagrams, put guards (true/simulated) earlier a state, add together actions triggered by a state change, and even defer events to be processed at a later on time. These diagrams can go very text-heavy, and the conditions are usually written for one specific programming language. This may brand the diagrams complex to await at, but it tin also make programming (and testing) faster as there is less room for misunderstanding.

Components of  UML country diagram

UML land diagrams utilise a annotation that you may have already seen in our UML activity diagrams.

  • Starting time state: A solid circle.
  • Finish land: A solid circumvolve with a band around it.
  • Country: A rectangle with rounded corners, with the name of the activeness.
  • Transition: Connector arrows with a label to indicate the trigger for that transition, if in that location is one.
  • Land with internal activities: A land with a horizontal line, and the actions listed in the lesser half.
  • Composite state: A country with a horizontal line, and a nested state diagram (or link to another draw.io page) in the bottom half.
  • Guards or conditions: A diamond.
  • Fork: A thicker line with one transition incoming and two or more than approachable.
  • Join: A thicker line with more than than one transition incoming and but one outgoing.

Tip: UML 2 country diagrams use the clear black/white shapes, and UML ane has yellow shapes with a red outline. I personally discover UML 2 easier to read.

Enable the UML shape library

In draw.io, all the shapes you need for activity diagrams are in the UML shape library. ClickMore than Shapes … at the bottom of the left-hand panel, then enable theUML shape library and clickApply.

Find shapes even faster: Even if y'all don't accept the UML library displayed, you tin can search for "country" in the left-manus panel, and it will show y'all the shapes you need to create a state diagram (too as a few others which are related to 'states' in other diagram types).

The dissimilar betwixt forks/joins and guards

Guards represent decisions based on conditions, similar to those in menses charts. In the simple example below, the baby-sit checks whether fees have been paid. If yes, information technology continues to the next country. If no, it aborts the entire enrollment.

Forks and joins occur where there are two or more parallel things beingness done in a program – concurrent states. If there is a fork, there will be a corresponding bring together where the different 'threads' will synchronize. In the simple example below, both undergraduate and postgraduate students are assigned a student ID (university administration), which happens at the aforementioned time as assigning a postgrad supervisor (faculty administration).

UML state diagram for the Habit Tracker example

Standing with the aforementioned instance used in the previous UML diagram posts,  I've created a state diagram for when a user is logged into the Addiction Tracker. You can se that it is like to the activeness diagram (displayed after the land diagram).

Because this example is simple, and contains no parallel actions or states, information technology wouldn't make sense to employ both types of diagrams. In reality, most software is highly complex, and sometimes country diagrams volition be needed in addition to (or instead of) activity diagrams.