UML Statecharts can be very effective modeling tools for describing systems and software requirements. They provide a clear framework for identifying business rules. The same business rules often apply to multiple states – defining a commonality for those states. There is an element called a substate in UML statecharts that can be used to make it more obvious that a particular business rule applies to multiple states.
A Simple UML Statechart
We documented business rules with a UML statechart in a previous article, that describes the possible states of a customer order. Those states are saved, placed, charged, shipped, delivered, and cancelled.
We defined a series of transitions and talked about the rules associated with those transitions. Imagine that the business rules changed, and customers were allowed to cancel orders even after they had been shipped (as long as they hadn’t been delivered).
Commonality In UML Statechart States
This could serve as an indication that there is a relevant commonality between the states: placed, charged, and shipped. All of these states represent an order that is “in process.” The customer has officially asked us for something, we have not delivered it yet (from the customer’s perspective), and the order has not been cancelled.
You can think of our states as having a hierarchy. You can create a state called “In Process” that generalizes the three states placed, charged, and shipped. By introducing this notion of generalization, we create “substates.”
UML Statecharts allow you to draw these substates inside of a larger superstate. The superstate is drawn with a rounded-corner rectangle, like the other states – but it includes a horizontal bar at the top that allows you to easily show the name of the superstate, separated from the information describing the substates.
Notice that the transitions between the substates are the same ones that were defined when the “In Process” superstate did not exist. You can have transitions between substates, and to and from substates to other states that are outside of the superstate. You can also have transitions that go directly to or come directly from the superstate.
Notice the single “cancel order” transition that comes from the superstate, In Process. This represents that the transition can be from any of the substates of In Process. The “deliver order” transition can only come from the Delivered substate. The transition “submit order” goes directly from the Saved state to the Placed substate.
Summary
The technique of using substates within UML statecharts allows for cleaner documentation when there are notions that present commonality (like business rules) that justify generalizing multiple states into a single state. This technique allows you to reduce the number of business rules that you must maintain, and makes the diagram easier to read.