π State Machines and State Diagrams (OOAD)
πΉ 1. Definition
A State Machine (Finite State Machine) is a behavioral model that describes how an object changes its state in response to events over time.
π Simple idea:
It shows what an object is doing at any point in time and how it moves from one condition to another.
A State Diagram (UML State Machine Diagram) is the graphical representation of this behavior.
πΉ 2. Key Idea
- Objects do not behave the same all the time
- They exist in different states
- Events cause transitions between states
πΉ 3. Real-Life Example
π Example: ATM Machine
- Idle β Card Inserted β PIN Entered β Transaction β Idle
Each step is a state, and actions move it forward.
πΉ 4. Components of State Machine Diagram β
πΈ 4.1 State
A condition or situation of an object.
Example:
- Idle
- Processing
- Active
- Locked
π Represented as rounded rectangles.
πΈ 4.2 Initial State
- Starting point of the system
- Shown as a black dot (β)
πΈ 4.3 Final State
- End of process
- Shown as a bullseye (β)
πΈ 4.4 Transition
- Movement from one state to another
- Triggered by an event
Example:
Idle β (Insert Card) β Active
πΈ 4.5 Event
- Something that causes a change
Example:
- Button press
- Login request
- Timeout
πΈ 4.6 Action
- Activity performed during transition
πΉ 5. State Diagram Notation
β β Initial State
β β Final State
[State] β Rounded rectangle
β β Transition arrow
πΉ 6. Example: ATM System β
πΉ States:
- Idle
- Card Inserted
- PIN Verification
- Transaction
- Exit
πΉ Transitions:
Idle β Insert Card β Card Inserted
Card Inserted β Enter PIN β PIN Verification
PIN Verification β Success β Transaction
Transaction β Finish β Idle
πΉ 7. Example Diagram Description
- Start at Idle (β)
- Card inserted β move to next state
- PIN entered β system verifies
- Transaction happens
- Ends or returns to Idle
πΉ 8. Types of States
πΈ 8.1 Simple State
πΈ 8.2 Composite State
πΈ 8.3 Concurrent State
- Multiple states active at the same time
πΉ 9. State vs Activity Diagram
| Feature |
State Diagram |
Activity Diagram |
| Focus |
Object states |
Workflow/process |
| Behavior |
Event-driven |
Action-driven |
| Level |
Object level |
System level |
πΉ 10. Where State Diagrams are Used
- ATM systems
- Traffic lights
- Online shopping carts
- Vending machines
- User login systems
πΉ 11. Advantages of State Diagrams
- Shows dynamic behavior clearly
- Helps in system understanding
- Useful for real-time systems
- Detects invalid states
- Improves design accuracy
πΉ 12. Important Rules / Guidelines
- Identify all possible states first
- Define events carefully
- Avoid unnecessary states
- Ensure all transitions are valid
- Always define initial and final state
πΉ 13. Likely Exam Questions
- Define state machine in OOAD.
- What is a state diagram?
- Explain components of state machine diagram.
- Draw state diagram of ATM system.
- Difference between state and activity diagram.
- What is a transition?
- Explain initial and final state.
- What are events in state diagrams?
- Types of states in UML.
- Advantages of state diagrams.
πΉ 14. Quick Revision Summary π§
- State Machine = Behavior of object over time
- State = Condition of object
- Event = Trigger for change
- Transition = Movement between states
π Trick to Remember:
"SET" β State, Event, Transition