Object-Oriented Modeling Using UML CS 3331 Sections 2.1 and 2.2 Chapter 5 of UML@Classroom (Martina Seidl, et al., Springer 2015; ebook) Outline State machine diagram Basic elements Protocol vs behavior state machines Structuring state machines Sequence diagram 2 Dynamic Models Used to model control aspects (e.g., when) Several different models possible, e.g., State machine diagram: focusing on state changes Interaction diagram: focusing on interactions Sequence diagram: time order of interactions between objects Communication diagram: messages passed between objects 3 State Machine Diagram Depicts the flow of control using states and transitions by describing Generalization of finite state machines How an object or system changes over time Event (e.g., operation calls) it may respond to How it respond to them Capture state-dependent behavior precisely Good for modeling reactive systems Passive nature (vs active) 4 Modeling Object States Graph representing finite state machine Network of states and transitions button pressed On Off button pressed 5 Elements of State Machine Diagram Final state Initial state Transition Running Idle State 6 State Condition or situation in the life of a system (or object) during which it: Satisfies some condition, Performs some activity, or Waits for some events. Set of values of properties that affect the behavior of the system (or object). Abstraction over object’s concrete fields Determines response to an event, Thus, different states may produce different responses to the same event 7 State (Cont.) Examples The invoice is paid. The car is parked. The engine is running. Kate is working. Jim is playing. All ships are placed. A Game is over. The system is waiting for a player’s move. 8 Transition Relationship between two states indicating that a system (or object) in the first state will: Perform certain actions and Enter the second state when specified event occurs and specified condition is satisfied. Consists of: Source and target states Optional event, guard condition, and action Event [Condition] / Action Source Target 9 Definition Event Action An occurrence of a stimulus that can trigger a state transition Instantaneous and no duration An executable atomic computation that results in a change in state of the model or the return of a value Run to completion Guard Side condition enabling/preventing a transition keyPressed(k) [k == ENTER_KEY] / buf := input 10 Example pressed/turn on light Off On pressed/turn off light Q: Model a night light that automatically turns on and off? 11 Outline State machine diagram Basic elements Protocol vs behavior state machines Structuring state machines Sequence diagram 12 Protocol vs. Behavior Machines Protocol state machine Specifies the intended pattern of calls on an object by specifying which operations of the object can be called in which state and under which condition, thus specifying the allowed call sequences on the object's operations. Describes an object's life cycle. Post-conditions are used instead of actions Behavior state machine Expresses the behavior of part of a system, e.g., to define object and operation behavior Actions are used instead of post-conditions States can have actions (entry, exit, do) 13 Protocol State Machine Account deposit(x) -balance: int + withdraw(x: int): void + deposit(x: int): void Not Overdrawn [balance >= 0] withdraw(x)[balance - x < 0] deposit(x)[balance + x >= 0] withdraw(x)[balance - x >= 0] Overdrawn [balance < 0] deposit(x)[balance + x < 0] 14 Behavior State Machine [i < N && a[i] != v] / i := i + 1 / k := -1; i = 0 Searching [i <= N && a[i] != v] [a[i] = v] / k := i [i >= N && a[i] != v] 15 In Class: Digital Watch (Pairs) Draw a state machine diagram The watch has a single mode button and a single advance button. Pressing the mode button once and then pressing the advance button increments the hours by 1. Each press of the advance button increments the hour. Pressing the mode button the second time allows advancing the minutes by one. Pressing the mode button a third time displays the current time. While displaying the current time, the advance button is ignored. Pressing the mode button allows the user to set the hour again. 16 More on States Optional internal activities Actions and activities performed while in the state Action vs activity: atomic vs interrupt-able Predefined: entry, exit, do Optional internal transitions Reaction within a state but without changing state Name Internal activities Internal transitions 17 Internal Activities and Transitions Internal actions/activities Entry: action performed on entry to state Exit: action performed on exit from state Do: activity performed while in the state Internal transitions Reactions to events that doesn’t cause state changes Attacking entry/unsheathe sword do/charge exit/sheathe sword enemy swings[distance<3 ft]/dodge 18 Example [number.isValid()] Starting entry / start dial tone exit / end dial tone dialed (n) Dialing entry / number.append(n) dialed (n) 19 Outline State machine diagram Basic elements Protocol vs behavior state machines Structuring state machines Sequence diagram 20 Structuring Diagrams Can this diagram be presented better? recovery success recovery failure anomaly Recovery Identification Normal temperature pressure recovery failure recovery success Pressure Recovery Temperature Recovery recovery failure recovery success 21 Composite States anomaly Recovery Normal recovery success recovery failure 22 Composite States (Cont.) anomaly Recovery Recovery Identification Normal recovery success recovery failure pressure Pressure Recovery temperature Temperature Recovery 23 Composite States (Cont.) Used to simplify diagrams Inside, looks like state machine diagram May have composite transitions May have transitions from substates Sequential (OR) and parallel (AND) states 24 Composites and Transitions Transition to/from composite state Active Idle Validating Selecting Processing Maintenance Printing Transition from substate 25 Exercise Model a night light that Automatically turns on and off when it becomes dark or bright. Has a button to select a light color: white, blue, and red. The initial color is white, and pressing the button while the light is on changes it to the next color. 26 Parallel Composition Concurrency (split of control) Synchronization Superstate substate1 substate2 substate3 substate4 27 Example 28 Exercise Model a night light that Automatically turns on and off when it becomes dark or bright. Has a button to select a light color: white, blue, and red. The initial color is white, and pressing the button changes it to the next color in the color sequence. The button works regardless whether the light is on or off. 29 Outline State machine diagram Sequence diagram 30 Sequence Diagram Describes a sequence of method calls among objects Highlights the time ordering of method calls 31 Example : Cus tom er object : Order : Paym ent : Product : Supplier place an order proces s control lifetime validate if ( paym ent ok ) deliver if ( not in s tock ) back order get addres s m ail to addres s message 32 Example (Cont.) : Cus tom er : Order : Paym ent : Product : Supplier place an order proces s validate Sequence of message sending if ( paym ent ok ) deliver if ( not in s tock ) back order get addres s m ail to addres s 33
© Copyright 2026 Paperzz