Download presentation
Presentation is loading. Please wait.
Published byDarren Carr Modified over 9 years ago
2
CS 325: Software Engineering February 26, 2015 Object-State Modeling for Event-Driven Systems Event-Driven Systems UML State Machine Diagrams State Pattern
3
CS 325 February 26, 2015 Page 127 Event-Driven Systems Some software systems are dependent on particular events occurring in order to set up the necessary conditions for specific processing to progress. Consider a digital watch system… The watch has four modes: display, set-alarm, stopwatch, and set-time-and-date. The watch has three buttons: mode (for going to the four modes), start-stop (for advancing values in the different modes), and light (for selection, lighting, and stopping the buzzer). Essentially, the entire system can be viewed as a system of events that allow the system to transition between various states. Starting in display mode, pressing the mode button repeatedly takes the watch to set-alarm mode, to stopwatch mode, to set- time-and-date mode, and back to display mode, in that order. For each mode, the light button is used to step through the values that can be set (e.g., in set-time-and-date mode, set the hour, then the minute, then the second, then the month, then the day, and finally the day of the week).
4
CS 325 February 26, 2015 Page 128 UML State Machine Diagrams State Machine Diagrams show the states of an object and the transitions that can cause a change in state. This example models the login process for an on- line banking system.
5
CS 325 February 26, 2015 Page 129 UML State Machine Diagrams A state machine diagram for the digital watch:
6
CS 325 February 26, 2015 Page 130 State Pattern Conventional means of implementing state machines have serious drawbacks… Switch (mode) { Case Display: Case Display: Switch (button): Switch (button): { Case Mode: Case Mode: Case StartStop: Case StartStop: Case Light: Case Light: } Case SetAlarm: Case SetAlarm: Switch (button): Switch (button): { Case Mode: Case Mode: Case StartStop: Case StartStop: Case Light: Case Light: } Case Stopwatch: Case Stopwatch: Switch (button): Switch (button): { Case Mode: Case Mode: Case StartStop: Case StartStop: Case Light: Case Light: } Case SetTimeAndDate: Case SetTimeAndDate: Switch (button): Switch (button): { Case Mode: Case Mode: Case StartStop: Case StartStop: Case Light: Case Light: }} For example, the nested-switch approach is difficult to comprehend, to test, to maintain, and especially to modify.
7
CS 325 February 26, 2015 Page 131 State Pattern The State Pattern is a design pattern to address the implementation of a state machine.
8
CS 325 February 26, 2015 Page 132 State Pattern The State Pattern localizes state-specific behavior in an individual class for each state, and puts all of the behavior for that state in a single object, eliminating the necessity for a set of long, look-alike conditional statements scattered through the program’s code. By aggregating state-specific behavior into state classes with intention-revealing names, it’s easier to find that specific logic, and it’s all in one place, improving cohesion. Extending the behavior is just a matter of defining additional state classes and modifying the linkages between the states.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.