SWEN-261 Introduction to Software Engineering
Department of Software Engineering Rochester Institute of Technology
State-based Behavior II SWEN-261 Introduction to Software - - PowerPoint PPT Presentation
State-based Behavior II SWEN-261 Introduction to Software Engineering Department of Software Engineering Rochester Institute of Technology There are a number of benefits for explicitly modeling state-based behavior. Explicitly defining
Department of Software Engineering Rochester Institute of Technology
2
3
if (pe.pinsDownOnThisThrow() >= 0) { if (frameNumber == 9) { if (pe.totalPinsDown() == 10) { if(pe.getThrowNumber() == 1) { if ((pe.totalPinsDown() != 10) && (pe.getThrowNumber() == 2 && tenthFrameStrike == false)) { if (pe.getThrowNumber() == 3) { if (pe.pinsDownOnThisThrow() == 10) { } else if (pe.getThrowNumber() == 2) { } else if (pe.getThrowNumber() == 3) { if( i%2 == 1 && curScore[i - 1] + curScore[i] == 10 && i < current - 1 && i < 19){ if (i > 1) { } else if( i < current && i%2 == 0 && curScore[i] == 10 && i < 18){ if (curScore[i+2] != -1) { if(curScore[i+3] != -1) { } else if(curScore[i+4] != -1) { if (strikeballs == 2){ if(curScore[i+1] != -1) { if (curScore[i+2] != -1){ if( curScore[i+2] != -2){ if( curScore[i+3] != -2){ if ( i/2 > 0 ){ if (curScore[i+3] != -1){ if( curScore[i+3] != -2){ if( i%2 == 0 && i < 18){ if ( i/2 == 0 ) { if(curScore[i] != -2){ } else if (i/2 != 9){ if(curScore[i] != -2){ } else if (i < 18){ if(curScore[i] != -1 && i > 2){ if(curScore[i] != -2){ if (i/2 == 9){ if (i == 18){ if(curScore[i] != -2){ } else if (i/2 == 10) { if(curScore[i] != -2){
4
5
… early on you don't feel like your objects' state machine behaviour is complex enough to warrant a "full-blown" state machine (YAGNI and all that jazz), but later on – when it IS complex enough – you feel like you've invested too much time/effort to replace it with something that has equivalent functionality. It's a bit
Alan Skorkin Why Developers Never Use State Machines
6
7
8
9
10
/** * States for the game in a state-based implementation. */ private enum State {WAIT_FOR_GUESS, GAME_WON, GAME_LOST} /** * The current state the object is in. */ private State currentState = State.WAIT_FOR_GUESS; 11
public synchronized boolean makeGuess(final int myGuess) { boolean validGuess = false; switch (currentState) { case WAIT_FOR_GUESS: if (myGuess >= 0 && myGuess < UPPER_BOUND){ howManyGuessesLeft--; if (myGuess == numberToGuess) { currentState = State.GAME_WON; } else if (howManyGuessesLeft <= 0) { currentState = State.GAME_LOST; } validGuess = true; } break; // Guesses are expected only when the game is in WAIT_FOR_GUESS. default: throw new IllegalStateException("No more guesses allowed."); } return validGuess; } 12
This state machine has only one event—a call to makeGuess(). You need to implement code for each state where this event triggers a transition. What you do if the event is received when you do not expect it, is application dependent. You typically implement guards as the conditional of an if statement. The transition is completed by setting the current state to the next state.
13