29.3. How to Apply State Machine Diagrams?
State-Independent and State-Dependent Objects
If an object always responds the same way to an event, then it is considered state-independent (or modeless) with respect to that event. For example, if an object receives a message, and the responding method always does the same thing. The object is state-independent with respect to that message. If, for all events of interest, an object always reacts the same way, it is a state-independent object . By contrast, state-dependent objects react differently to events depending on their state or mode.
Guideline Consider state machines for state-dependent objects with complex behavior, not for state-independent objects. |
Guideline In general, business information systems have few complex state-dependent classes. It is seldom helpful to apply state machine modeling.By contrast, process control, device control, protocol handlers, and telecommunication domains often have many state-dependent objects. If you work in these domains, definitely know and consider state machine modeling. |
Modeling State-dependent Objects
Broadly, state machines are applied in two ways:
- To model the behavior of a complex reactive object in response to events.
- To model legal sequences of operationsprotocol or language specifications.
- This approach may be considered a specialization of #1, if the "object" is a language, protocol, or process. A formal grammar for a context-free language is a kind of state machine.
Complex Reactive Objects
- Physical Devices controlled by software
- Phone, car, microwave oven: They have complex and rich reactions to events, and the reaction depends upon their current mode.
- Transactions and related Business Objects
- How does a business object (a sale, order, payment) react to an event? For example, what should happen to an Order if a cancel event occurs? And understanding all the events and states that a Package can go through in the shipping business can help with design, validation, and process improvement.
- Role Mutators These are objects that change their role.
- A Person changing roles from being a civilian to a veteran. Each role is represented by a state.
Protocols and Legal Sequences
- Communication Protocols
- TCP, and new protocols, can be easily and clearly understood with a state machine diagram. The diagram illustrates when operations are legal. For example, a TCP "close" request should be ignored if the protocol handler is already in the "closed" state.
- UI Page/Window Flow or Navigation When doing UI modeling, it can be useful to understand the legal sequence between Web pages or windows; this is often complex. A state machine is a great tool to model UI navigation.
- UI Flow Controllers or Sessions This is related to UI navigation modeling, but specifically focused on the server-side object that controls page flow. These are usually server-side objects representing an ongoing session or conversations with a client. For example, a Web application that remembers the state of the session with a Web client and controls the transitions to new Web pages, or the modified display of the current Web page, based upon the state of the session and the next operation that is received.
- Use Case System Operations
- Do you recall the system operations for Process Sale: makeNewSale, enterItem , and so forth? These should arrive in a legal order; for example, endSale should only come after one or more enterItem operations. Usually, the order is trivially obvious, but if complex, a state machine can model this, treating the use case itself as an object.
- Individual UI Window Event Handling
- Understanding the events and legal sequences for one window or form. For example, the Edit-Paste action is only valid if there is something in the "clipboard" to paste.