A Student Guide to Object Oriented Development Chapter
A Student Guide to Object. Oriented Development Chapter 7 State Diagrams 1
State diagram In sequence diagrams you are looking at all the classes affected by one use case In state diagrams you are looking at one class across all use cases and how it is effected by the events in the system 2
State diagram l State diagrams are increasingly widely used to model the ways that a system can respond to external events l All objects of the class (the individual customers or products) have the same range of ways in which they can behave, but the actual way an individual object does behave during the running of the system depends on the sequence of events that it experiences. l State diagrams look at how a class is affected by the different use cases in the system and how the objects of the class behave in response to events that affect them. They model the behaviour of one object across several use cases. 3
State Diagram l A state diagram only ever describes the behaviour of a single class illustrating how its objects behave during their lifetimes and how they react to all uses cases in which they are involved. l State diagrams also model the order that events affect an object in e. g. in the Wheels system the bike must be assigned a number before it can be hired l In most systems only a few of the classes are complicated enough to merit a state diagram. These are classes where the response of an object of the class to a particular event varies depending on the state the object is in at the time l State diagrams are also referred to as state charts and state transition diagrams 4
States and Events l State - The state of the object here refers to the situation it is in while satisfying some condition (such as a bank account having some money) or waiting for an event (such as someone trying to withdraw or deposit money). l Event - An event is something that happens which has significance for the system and affects an object of at least one of the system’s classes. We can tell if an object is in a particular state by looking at the values of some of its attributes and its links to other objects. 5
The state of an object l We can tell if an object is in a particular state by looking at the values of its attributes For example, if a Bank Account object is in credit the value of the balance attribute will be a positive amount or zero, but if it is overdrawn the value of balance will be negative. l In the Wheels case study, we can tell if a bike is hired out because there will be a link from the Bike object to an active Hire object. 6
state start state stop state transition from one state to another self-transition (no change of state) event [guard] / action transition label (each of the three parts is optional) 7
State diagram for a bank account class Self transition state Event fires diagram Start a transition stop state Guard for a bank account class state 8
In the "In credit" state 9
State Model l State Model describes the sequence of states that an object goes through during its lifetime in response to events. l Includes the objects responses to events – Actions • Single act performed by the object – Activities • Continuous set of actions performed by an object UML and C++ A Practical Guide To Object-Oriented Development
State l This represents a stage in the lifecycle of an object. l Technically the period of time during which an object is waiting for an event to occur. l The internal state of an object is the combination of the data values of the attributes of object. UML and C++ A Practical Guide To Object-Oriented Development
Event l An event is a condition that can be detected by an object. l From an programmatic perspective – An event is a one way asynchronous transmission of information from one object to another. – The information may have parameters with names and types as part of the message sent UML and C++ A Practical Guide To Object-Oriented Development
Transition l. A transition is a response by an object to an event received by it. – The response can be • a change to a new object state • a change to the same object state • the performance of some action l The ability of the object to respond to a given event is described by guard conditions. UML and C++ A Practical Guide To Object-Oriented Development
Action An action is an activity or operation that is done inside a state or on a transition between states l An action is l – atomic – instantaneous l An action can occur – during a transition – on entry to a state, – during the entire period an object is in a state, – on exit from a state, UML and C++ A – Practical on arrival Guide To of an event that does not cause a state Object-Oriented transition Development
Activity l An activity is an operation or set of operations that executes during the entire time an object is in a state. l An activity is not atomic. l An activity can be interrupted by an event while it is executing UML and C++ A Practical Guide To Object-Oriented Development
Events for Bike Object 16
Completed state diagram for Bike Object 17
State diagrams – key words ‘after’ – used to show an event taking place after a specified time period e. g. after [6 months] / delete ‘when’ – event takes place when a condition is satisfied e. g. when [all items in stock] / fill order 18
State diagrams – entry and exit events off. Hook entry / start dialling tone exit / stop dialling tone do / emit dialling tone Keyword ‘do’ – behaviour that lasts for the duration of a state is called an activity, it is modelled using the keyword ‘do’ 19
Nested State Diagram UML and C++ A Practical Guide To Object-Oriented Development
Nested Concurrent Substates UML and C++ A Practical Guide To Object-Oriented Development
Nested Concurrent Substates UML and C++ A Practical Guide To Object-Oriented Development
Mealy model for Microwave Oven UML and C++ A Practical Guide To Object-Oriented Development
Moore model UML and C++ A Practical Guide To Object-Oriented Development
- Slides: 24