0% found this document useful (0 votes)
34 views

Lecture 5 - State Chart Diagram

Uploaded by

devmith2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Lecture 5 - State Chart Diagram

Uploaded by

devmith2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

ICT1411 Object-Oriented Programming

Lecture 5: State Chart Diagrams


State Chart Diagram
• Objects have behaviors and states.
• The state of an object depends on its current activity or condition.
• A state chart diagrams shows the possible states of the object and the transitions that
cause a change in state.
State chart Diagrams
Difference between state chart diagrams and Activity diagrams:

• 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

• A state chart diagram is composed by three components


o State
o Transitions
o Events
Example 1
Event ?
• Basic light switch with two states: "On" and "Off."
• States:
o On: The light is turned on.
o Off: The light is turned off. Event ?
• Events:
o Switch On: The event of flipping the light switch up. State ? State ?
o Switch Off: The event of flipping the light switch down.
• Transitions:
o Switch On: Transition from the "Off" state to the "On" state in response to the "Switch On" event.
o Switch Off: Transition from the "On" state to the "Off" state in response to the "Switch Off" event.
Example 2
State is defined as a snapshot or a milestone of
an object’s behavior at a particular point in time.
On
Boot
Process
Computer
Idle
Shut Down
Off
State chart Diagram Components
State
• A state chart diagram can include zero or more start states.
• A state chart diagram can include more than one end states.
• There are three separate notations for state.
▪ State
▪ End state
▪ Start state
State chart Diagram Components
State

Start state symbol signals the first step of a process.

End state symbol stands for the result of a process.


Types of States
The UML consist of three states:
1. Simple state
o It does not constitute any sub state.
o Fundamental building blocks of a state chart diagram.
o State defines current condition of an event or activity. State diagram is often used to
describe state changes triggered by events.

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

• Transition takes operation from one state to


another and represents the response to a
particular event.
• A single transition comes out of each state State A State B
or activity, connecting it to the next state or
activity.
State chart Diagrams Components
Decision Points
Decision points make the diagram more visually appealing by grouping
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.

You might also like