Object Behaviours UML Statechart Diagrams Software Requirements and
Object Behaviours UML Statechart Diagrams Software Requirements and Design CITS 4401 Lecture 11 Based on lecture notes by Bruegge & Dutoit Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 1
Finite state machine flip switch on Light off flip switch off Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 2
UML Statechart Diagrams • A UML statechart diagram is a notation for describing the sequence of states an object goes through in response to external events. • UML state machines are extensions of the finite state machine model. • A state is a condition satisfied by the attributes of an object. • A transition indicates a move from one state to another • An event is a potential trigger for a change of state Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 3
State machine Transition State flip switch on Light off flip switch off Event Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 4
Ex 1. Interaction with PC (V 1) ctrl-alt-del Blue screen Login screen confirm-logout username-passwd Logout screen Active desktop request-logout Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 5
UML Statecharts • State machine models show individual objects change their state in response to events. They are represented in the UML using statechart diagrams • While sequence diagrams are used to model the combined behaviour of a group of objects in the system, the statechart diagrams are used to model the behaviour of a single object in response to external events. • In the context of behaviour modelling, 2 different characterizations of states must be considered: 1. the state of each class, and 2. the state of the system as observed from the outside as the system performs its function. Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 6
Interaction with PC (V 2) Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 7
UML Statechart Diagram Notation State 1 Event 1(attr) [condition]/action do/action Event trigger With parameters event/action State 2 Guard condition entry/action exit/action Also: internal transitions • UML notation based on statecharts by Harel in 1987 – Added are a few object-oriented modifications • A UML statechart diagram (or UML state machine) can be mapped into a finite state machine (FSM) Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 8
Statechart Diagrams • Graphs whose nodes are states and whose directed arcs are transitions labelled by events. • States capture conditions which hold for a period of time – e. g. light is on, light is off • Events change the state (except internal) – e. g. turning the light on, turning the light off • Statechart diagrams represent behaviour from the perspective of a single object only – An object model with a set of objects must be represented by a set of state diagrams Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 9
State • An abstraction of the attributes of a class – State is the aggregation of several attributes of a class • Basically an equivalence class of all those attribute values and links that do not need to be distinguished as far as the control structure of the system is concerned – Example: State of a user interface screen • logged in, logged out, active, idle • active is an abstraction of all the user’s logged in activity • State has duration Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 10
Event • Something that happens at a point in time (e. g. button press, mouse click) • Triggers a transition – Internal transition (no state change) – External transition (change to different state) • May result in an action being executed Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 11
Example 2: vending machine coins_in(amount) / set balance Idle Collect Money coins_in(amount) / add to balance cancel / refund coins [item empty] [select(item)] [change<0] do: test item and compute change [change=0] do: dispense item Department of Computer Science & Software Engineering [change>0] do: make change CITS 4401 Software Requirements & Design | 12
Another example incident. Documented incident. Handled incident. Archived States of the Incident object of FRIEND Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 13
Problem Statement: Direction Control for a Toy Car ¡ Power is turned on ¡ Power is turned off l Car moves forward and car headlight shines ¡ Power is turned off l Car stops and headlight goes out. ¡ Power is turned on l Headlight shines ¡ Power is turned off l Headlight goes out. ¡ Power is turned on l Car runs forward with its headlight shining. l Car runs backward with its headlight shining. Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 14
Toy Car: Dynamic Model Wheel Headlight Stationary Off power on Stationary Forward power off power on Stationary On power on Stationary power Department of Computer Science & Software Engineering off power off Backward Stationary power on CITS 4401 Software Requirements & Design | 15
Practical Tips for Dynamic Modeling • Construct dynamic models only for classes with significant dynamic behavior – Avoid “analysis paralysis” • Consider only relevant attributes – Use abstraction if necessary • Look at the granularity of the application when deciding on actions and activities • Reduce notational clutter Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 16
State Chart Diagram vs Sequence Diagram • State chart diagrams help to identify: – Changes to objects over time • Sequence diagrams help to identify – The temporal relationships between objects over time – Sequence of operations as a response to one or more events Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 17
When to use state diagrams [Fowler] • State diagrams are good at describing the behavior of an object across several use cases. • State diagrams are not very good at describing behavior that involves a number of objects collaborating. • Therefore it is useful to combine state diagrams with other techniques. • If you do use state diagrams, don’t try to draw them for every class in the system. … it is almost always a waste of effort. • Use state diagrams only for those classes that exhibit interesting behaviour, where building the state diagram helps you understand what is going on. • Many people find that User Interface and Control objects have the kind of behaviour that is useful to depict with a state diagram. Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 18
Review (1): Requirements Analysis Functional Modelling • 1. What are the transformations? – Create scenarios and use case diagrams • Talk to client, observe, get historical records, do thought experiments • 2. What is the structure of the system? – Create object and class diagrams • Identify objects. What are the associations between them? What is their multiplicity? • What are the attributes of the objects? Object Modelling • What operations are defined on the objects? Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 19
Review (2): Requirements Analysis • 3. What is its control structure? – Create state diagrams • Only for the dynamically interesting objects. Dynamic Modelling – Create sequence diagrams • Identify senders and receivers of events • Show sequence of events exchanged between objects. Identify event dependencies and event concurrency. Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 20
When is a model dominant? • Functional model The model performs complicated transformations such as difficult computations consisting of many steps. • Object model The system has non-trivial data structures. • Dynamic model The model has many different types of events: Input, output, exceptions, errors, etc. Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 21
When is a model dominant? Examples • Compiler: Functional model most important. Dynamic model is trivial because there is only one type input and only a few outputs. • Database systems: Object model most important. Functional model is trivial, because their purpose is usually only to store, organize and retrieve data. • Spreadsheet program: Functional model most important. Object model is trivial, because the spreadsheet values are trivial and cannot be structured further. The only interesting object is the cell. Dynamic model is also relatively important. Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 22
Recommended reading UML Distilled by Martin Fowler, Chapter 10 https: //my. safaribooksonline. com/book/software-engineering-anddevelopment/uml/0321193687 q “Object oriented software engineering” by Bruegge & Dutoit q Chapter 5 q “Software Engineering” by Pressman Chapter 8, Section 8. 5 Department of Computer Science & Software Engineering CITS 4401 Software Requirements & Design | 23
- Slides: 23