liveBook · Manning

State transition

As explained in Object-Oriented Software Design in C++

A state transition is a change from one object state to another caused by a change in member-variable values.

A state transition changes an object from one state to another when the values of its member variables change. Member functions determine the runtime behaviors that cause those changes. section 1.8

A UML state diagram makes one object’s runtime behavior visible: rounded boxes represent states, arrows represent transitions, and labels identify the events or conditions that cause them. The diagram helps check whether a class has the necessary member variables and member functions. section 4.5

A transition may be caused by an external action, an internal operation, or a condition:

  • Starting a car engine transitions it from parked to idling; accelerating can then transition it to a state characterized by a new speed and heading. section 4.5
  • A ticket machine transitions from READY to VALIDATING when a customer inserts a card, then to TICKET_SOLD when validation succeeds. section 13.1
  • An invalid card returns the machine to READY; taking the last ticket transitions it to SOLD_OUT, which has no outgoing transitions. section 13.1
  • A student-enrollment diagram can show decisions, branches, merges, forks, and joins around the transitions involved in enrolling in a course. section 4.5

When many actions depend on the current state, the State Design Pattern assigns each state its own class. The current state object handles every action and returns the next state; an unreasonable action can return the current state unchanged. This keeps state-specific behavior encapsulated, makes the current state explicit, and reduces dependencies between states. section 13.1.1 section 13.1.3

Ticket-machine transition trace

With three tickets remaining, inserting a card produces READY → VALIDATING. Checking a valid card produces VALIDATING → TICKET_SOLD. Taking the ticket decreases the count and returns to READY. Taking the final ticket instead produces TICKET_SOLD → SOLD_OUT.

Figure 4.14 A UML state diagram that shows the state changes of a Car object during run time. Each box represents a state, and each arrow represents a transition from one state to another. Each transition is labeled with the behavior (event) that causes the transition. Such a diagram allows us to visualize what happens to a particular object during run time and helps to ensure that we designed the class with the correct member variables and member functions.

Use the UML diagram to trace the Car object’s states, identify the event on each transition, and check whether the class design supports the visible behavior. section 4.5

How Object-Oriented Software Design in C++ covers it

How deep it goes
Explains member-variable changes, UML notation, guarded paths, terminal states, and polymorphic next-state handling.
Examples
Uses Car behavior, student enrollment, and a ticket machine with card validation, ticket counts, and a SOLD_OUT state.
What it assumes
Assumes basic classes and member variables while introducing runtime state, behavior, encapsulation, inheritance, and polymorphism.
What you can do after
Evaluate/understand runtime state changes and design a state-based class using UML diagrams or separate State subclasses.
How it connects
Connects state transitions with UML state diagrams, encapsulation, member functions, polymorphism, and the State Design Pattern.

Also explained in

Compare how 7 books explain state transition →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.