Software Design Case Study - Sky DivingThe ProblemThis problem was devised specifically to teach the principles of designing a Mealy state machine. The assignment is to implement a state machine of states that a skydiver goes through. The program is to be a dialog based application. The state machine in this project is implemented using a [State, Event] table. The state diagram is:
For the Scream and Hurl Insult events, a random message would be displayed. The Exit state allows the user to be prompted to exit the program. The DesignThe first task is to identify all the states and events. The second task is to generate the transition table. This gives the next state for each [state, event] pair. The user interface has to display the current state and allow the user to select an event to execute. No state has more that 3 exit transitions, so to be safe I decided on 4 buttons for the dialog box. That should be enough to accommodate any expansion. The buttons must change their text and event based on the current state, so each button was seen to have these attributes:
A structure was defined for each button, which would be filled by the state handler upon entering the state. Because this was a small application whose only purpose was to be a learning exercise, I did not take the extra step of isolating the state machine from the user interface. The state handler directly interacts with the user interface objects. In a real application, it would be better to use SendMessage() to instruct the UI to set the buttons. Each state was then seen to have these attributes:
Finally, the transitions were seen to have these attributes:
The information flow is as follows:
Assuming that the state machine is initialized, when the event occurs, the application calls the state machine's DoEvent(...) method. The Event Table (a.k.a. transition table) uses the [CurrentState, Event] as a composite key and finds the appropriate entry in the table. If there is an Event function defined, it calls the function. It then gets the NewState from the table and sets the state. The SetState() method uses the state table to get the button data, set the buttons, and execute the state function (if any). The pseudo-code for the logic is:
Program Starts (OnInitDialog) User clicks a button DoEvent( Event ) SetState( New State ) The ImplementationThe user interface is rather dry and unexciting - but then, the purpose of the assignment was to make a state machine, not a computer game. The state function could always be modified to show graphics. One other design weakness that I left undone was the fact that, since the state machine's data is static, there can only be one instance of the object. Any other instances will share the same static data. A Singleton design pattern could have been used to ensure only a single instance, but then again, the purpose of the exercise was just to learn how to design a state machine. Click here to get the C++ source code. Click here to get the executable program. Unit TestingThe state diagram was used as the basis for unit testing. For each state, each transition was tested to ensure that the correct events occurred. |