Matakuliah Tahun : Konsep object-oriented : 2009 STATE DIAGRAMS Pertemuan 19 A Student Guide to Object-Oriented Development Chapter 7 State Diagrams 3 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 4 State diagram • State diagrams are increasingly widely used to model the ways that a system can respond to external events • 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. • 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. 5 State Diagram • 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. • 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 • 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 • State diagrams are also referred to as state charts and state transition diagrams 6 States and Events • 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). • 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. 7 The state of an object • 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. • 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. 8 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) 9 State diagram for a bank account class Self transition depos it money stop state withdraw money [new balance >= 0] withdraw balance and close account open account and depos it money In credit it money [new state diagram Event fires depos balance >= 0] Start a transition state withdraw money [new balance < 0 and within overdraft limit] Guard for a bank account class depos it money [new balance < 0 and within overdraft limit] withdraw money [new balance < 0 and within overdraft lim it] Overdrawn 10 car sensed / check s paces car leaves[ capacity > s paces ] / inc. s paces car enters[ >= 1 s pace free ] / dec. s paces car enters / dec.s paces Em pty Spaces free Event/guard/ action las t car leaves[ s paces = capacity-1 ] / inc. s paces car leaves / inc. s paces car takes last space / dec. s paces superstate Full car park closed 11 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 12 State diagrams – entry and exit events offHook 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’ 13