Lecture 5 - State Chart Diagram
Lecture 5 - State Chart Diagram
• State chart diagrams are used to represent a single object and how its behavior
causes it to change state
• Activity diagrams are used to model the flow of activities or actions within a
system, particularly within a use case or business process.
What is a State?
• Lets take a Person class which has the attribute age.
• A person will have different states depending on the value of age.
E.g.
Age < 18 –child
Age >= 18 –Adult
Age >= 65 –Senior Citizen
State chart Diagrams:
Components
State Name
2. Composite state
o It consists of nested states (substates) and State name
internal activities.
o It does not contain more than one initial state
and one final state.
o Substates can be sequential or concurrent.
o Used to group related sub-states together,
allowing for a more organized and modular
representation of the system's behavior.
o May have transitions that apply only to the
composite state as a whole or transitions that
are delegated to its sub-states.
Composite State 1
State 1 State 2
Composite State 2
State 1 State 2
State 1
State B
State A
State 1 State 2
3. Submachine state
o The submachine state is semantically identical to the composite state, but it can be
reused.
o A submachine state is a state that refers to another state machine.
o It allows for the reuse of state machines within a state chart diagram.
o The referenced state machine can be a standalone state machine with its own set of
states and transitions.
State chart Diagrams Components
Transitions
State A
State B State C
State chart Diagrams Components
Synchronization
Synchronization bars are used to show where states need to catch up with or
wait up for others
State A
State B
State chart Diagrams Components
Actions and Events
• An action shows what behavior happens when an event occurs, there are five basic
action types:
o entry (action that occurs when state has been entered)
o exit (action that occurs when state is being abandoned)
o do (action that occurs when state is being occupied)
o event (used to specify the action that occurs when a specific event is fired)
State Chart Diagram Guidelines and Best practices
Best Practices
• A unique and understandable name should be assigned to the state transition that describes the
behavior of the system.
• Out of multiple objects, only the essential objects are implemented.
• A proper name should be given to the events and the transitions.
Guidelines
• Keep the diagram simple.
• Question ‘black-hole’ and ‘miracle’ states.
• Black-hole state: A state have transition/s into it but none out of it. This is a something that
should be true only of final states.
• Miracle state: A state has transitions out of it, but none into it. This is a something that
should be true only of starting points.
Activity
Book
- isbn : int
- title : String
- price : float
+ borrowBook() : void
+ returnBook() : void
+ deleteBook() : void
Exercise 1
• A state chart for an online shopping cart with states "Empty,"
"Active," and "Checkout" and events for adding items, removing
items, and completing the checkout process.
Exercise 2
• The Burglar alarm is initially in the state of resting. Then by setting the alarm, the burglar
alarm state may be changed to the state set. When the alarm is set, it may be turned off.
This will allow the alarm to rest. While the alarm is set it can be triggered, which will
make it ring. When the alarm rings, it can be turned off. Then the alarm is at rest.
Exercise 3
• In the initial state, the ATM awaits user interaction (Idle). Upon inserting a card,
the system progresses to the "Read Card" state. If the inserted card is considered
valid, the ATM proceeds to the "PIN Check" state. In the "PIN Check" state, the
user enters a PIN, initiating the "Checking PIN" state. If the entered PIN is correct,
the system advances to the "Withdraw" state, where the withdrawal request is
processed. Following a successful transaction, the system enters the "Cash
Dispensed" state. If the inserted card is deemed invalid, the ATM immediately
ejects the card and goes back to the idle state. In the case of an incorrect PIN
entered during the "Checking PIN" state, the system moves to the "Card
Retained" state and subsequently back to the idle state after card retention is
completed.
Exercise 4
• Company A has a bug tracking system. This system has a UserAccount class which
represents a user in the system. Once the System Administrator creates a user, a
UserAccount object will be created with a pending state. The UserAccount object
will be active only when the System Manager approves the user. If the System
Manager rejects the user, the UserAccount will be in inactive state. The System
Manager can call Activate User and Deactivate User to move the UserAccount
object to active or inactive state. The system administrator will periodically call
Delete User function to delete the UserAccount objects, which are in the inactive
state.
• Draw a statechart diagram for the UserAccount object.