State Diagrams State Diagrams State Diagrams
Total Page:16
File Type:pdf, Size:1020Kb
State Diagrams In UML, we specify the behaviour of the individual objects of a class through State Diagrams. They specify: how an object reacts when one of its methods (operations) is called. UML State Diagrams Central the notions of state and state transformation: ITNP090 - Object Oriented Software Design Dr Andrea Bracciali A system may assume different states at given times, and change its state Module Co-ordinator according to event occurrences (e.g., methods calls in an object oriented 4B86 [email protected] world). Dept. of Computing Science & Mathematics 153 Dept. of Computing Science & Mathematics 154 University of Stirling University of Stirling State Diagrams State Diagrams A state diagram describes a system that, at any given time, is in a certain Another "dynamical" diagram. state and can take part in an event, such as having one of its methods called. State diagrams support event-driven modelling: how a system responds to As a consequence of that event, the object can: internal and external events (based on Statecharts [Harel 87, 88]). – move to a new state, – perform actions, such as Not really detailed in terms of data-flow. • calling the methods of other objects, or • change the values of attributes. They are based on extended finite state machines. We say that the event triggers a transition. What a finite state machine (or finite state automaton) is ? We label the transition with the name of the event. Dept. of Computing Science & Mathematics 155 Dept. of Computing Science & Mathematics 156 University of Stirling University of Stirling Example State diagrams A State Diagram for a computer monitor: A transition may be labelled with • Triggering events, • Guards, i.e. boolean conditions that may enable/block the transition • Behaviour, i.e. actions to be performed before entering into the new state. Pseudostates direct transition flow and include: • Initial • Final [Condition 1] • Choice [Condition 2] • Fork • ... Dept. of Computing Science & Mathematics 157 Dept. of Computing Science & Mathematics 158 University of Stirling University of Stirling Example: an Account object Example: an Account object Let us look at this through the simple example of our Account class. The states that an Account object can go through can be shown in a The state of an Account object can change when we deposit or withdraw state diagram as follows. money. ? Dept. of Computing Science & Mathematics 159 Dept. of Computing Science & Mathematics 160 University of Stirling University of Stirling Example: an Account object Example: an Account object The filled circle is the start marker (initial state). It means that when The state diagram shows that when an object is in the nofunds state, a new object of class Account is created, it starts in the nofunds the only method that can be called is deposit. state. When in the state nofunds, we cannot withdraw money. Objects of class Account can accept calls of methods deposit and When the object receives a call of deposit, the object moves to the withdraw. funds state. Dept. of Computing Science & Mathematics 161 Dept. of Computing Science & Mathematics 162 University of Stirling University of Stirling Example: an Account object Example: an Account object There are three situations: When in the funds state, the object can receive more calls of deposit. These cause us to stay in the funds state. 1. sufficient funds: stay in funds state and decrement balance attribute; The effect of withdraw depends on the current state of the object. It 2. exactly correct funds: move to nofunds state and make balance can only be called in the state funds and its effect depends on whether attribute zero. there are sufficient funds to satisfy the request. 3. insufficient funds: we can regard this as an error, i.e. we should never receive a withdraw message in this circumstance. Dept. of Computing Science & Mathematics 163 Dept. of Computing Science & Mathematics 164 University of Stirling University of Stirling State diagrams State diagrams To keep things simple, we are only modelling correct situations, i.e. we Accordingly to the Design by Contract view, the pre-condition: are not showing error transitions in the diagram. amount<=balance They may greatly complicate the diagram. We distinguish between different correct behaviours by putting on the withdraw operation, does not require the Account class to guards on events, e.g. amount < balance specify behaviour when the pre-condition is not met. Note the connection with pre- and post-conditions. It is the responsibility of the client/caller to ensure that the pre- condition is satisfied. Dept. of Computing Science & Mathematics 165 Dept. of Computing Science & Mathematics 166 University of Stirling University of Stirling State diagrams Example: Library Case Study We can associate actions with each event. Let us now devise a state diagram for the Copy object that we had in the Library case study. An action can involve: • calling a method, • changing an attribute such as balance, that is we can have assignments . However, in the early stages, we do not want to put too much detail into a state diagram; the emphasis is on getting the main structure right. ? Dept. of Computing Science & Mathematics 167 Dept. of Computing Science & Mathematics 168 University of Stirling University of Stirling Example: Library Case Study Example: Library Case Study When a Copy object is initially created, it is in state onShelf and may The action of calling inform and borrow is shown by putting these calls in receive a borrow message. the label of the transition after a '/' and separating them by a semi-colon. As a result of receiving this message, the Copy object will (according to the sequence diagram) The call of borrow has a reference to the borrowing Member object as the – call the inform method of its associated Publication object, and parameter m. – call the borrow method of the Member object that is doing the borrowing. Dept. of Computing Science & Mathematics 169 Dept. of Computing Science & Mathematics 170 University of Stirling University of Stirling Example: Library Case Study Example: Library Case Study When the Copy object was created, the association with a Publication Instead of writing the actions on the transitions, we can write the actions object was set up. inside the state, BUT this means that actions are not specific of a transition, e.g., you cannot distinguish how you reach a state (by means of which transition) Rather, there is an implicit entry event when a state is entered and an exit event when it is left (actions can be associated to these events). That is shown in an implementation by the Copy object having a reference to UML does not indicate what the syntax of the actions should be. a Publication object as the attribute pub. States with associated actions are sometimes called active states (and, those We then use m and pub in the actions of calling other object’s methods. without actions, passive states). Dept. of Computing Science & Mathematics 171 Dept. of Computing Science & Mathematics 172 University of Stirling University of Stirling Example: Library Case Study Example: a Publication object Let us now look at a possible state diagram for Publication: The state diagram for Copy can be shown in either of the two following ways. What is this? Dept. of Computing Science & Mathematics 173 Dept. of Computing Science & Mathematics 174 University of Stirling University of Stirling Generating Code Generating Code Let us consider again an object of class Copy. The state diagrams have been used to define the behaviour of an object, e.g. an object of class Copy. Then the actual objects are expected to be compliant with such a defined behaviour, that is, state diagrams impose constraints on what methods are/are not allowed to do. Indeed, it is possible to generate the corresponding Java or C++ code. As we are following the idea of Design by Contract, we have made the decision that an unexpected message is just ignored. Hence, if a Copy is in state onLoan and the borrow method is called, it is ignored. Dept. of Computing Science & Mathematics 175 Dept. of Computing Science & Mathematics 176 University of Stirling University of Stirling Generating Code Generating Code class Copy { public void borrow(Member m) { // define the possible states // borrow private final static int onShelf = 0; if (currentState == onShelf) { private final static int onLoan = 1; pub.inform(this); private int currentState; m.borrow(this); private Publication pub; currentState = onLoan; } public Copy(Publication p) { } // constructor pub = p; currentState = onShelf; } Dept. of Computing Science & Mathematics 177 Dept. of Computing Science & Mathematics 178 University of Stirling University of Stirling Generating Code Generating Code public void returns(Member m) { We will know about the required parameters in the constructor from // returns the sequence diagram in which a Copy object was created. if (currentState == onLoan) { Note that the information in the generated code comes from several pub.returns(this); different diagrams. m.returns(this); currentState = onShelf; } A drawback of the code is that it is not really very readable by } humans. } // end Copy "However, we can regard the UML diagrams as our source program and the (automatically) generated Java or C++ as machine code". Dept. of Computing Science & Mathematics 179 Dept. of Computing Science & Mathematics 180 University of Stirling University of Stirling Generating Code State vs. Sequence Diagrams Idea is that the UML is at a higher more abstract level and so we are It should be emphasised that a state diagram defines the different better able to reason at that level of abstraction about the overall states that an object can go through when it is being executed. design. It does show the possible method calls that are made to carry out its It should therefore be easier to prove that the system, and hence actions.