Presentation is loading. Please wait.

Presentation is loading. Please wait.

State ● What is state? ABCDE. State and Data Structures ABCDE A B C D E ● Data structures encode state. In doing so, they introduce state ● State of data.

Similar presentations


Presentation on theme: "State ● What is state? ABCDE. State and Data Structures ABCDE A B C D E ● Data structures encode state. In doing so, they introduce state ● State of data."— Presentation transcript:

1 State ● What is state? ABCDE

2 State and Data Structures ABCDE A B C D E ● Data structures encode state. In doing so, they introduce state ● State of data ● State of encoding Introduced state

3 Representing State: Data Structures ● We choose data structures based on different criteria: ● Optimal access time ● Optimal manipulation time ● Ease of implementation ● Data Structures are a mechanism of encoding state ● For any state, there are many ways of encoding that state ● The act of encoding state creates new states ● Some encoding mechanisms introduce complex state ● It is necessary to develop skills in evaluating mechanisms of encoding state

4 State and Behavior – state variables ● Entities may behave differently depending on their state public class Person { boolean married; public boolean willYouGoOutWithMe(Person aPerson) { if (married) return false; else return evaluateDatePotential(aPerson); }

5 State and Behavior: Examine this code boolean flag; int i = 0; [...] while(i< some big number) { [...] if (flag) x = x + y*30; else x = x - 500; } boolean flag; int i = 0; int temp; [...] if (flag) temp = y*30; else temp = -500; while(i< some big number) { [...] x = x + temp; }

6 Polymorphism – A mechanism for encoding state ● In the previous slide, the state was defined by boolean. ● This encoding was not compatible with the computation that had to be done ● The if statement provides the mapping from the boolean representation of state to another form of state which is relevant to the use of the state ● In this case, the use of the state is in the form of computation. ● Simple Example: ● Assertions

7 State variables and state machines ● State machines are a very common way of computation ● Parsing ● Compilers ● Stateful Protocols ● When a state machine is built, there must be some manner of representing the current state of the state machine ● Often encoded as a state variable ● A common procedural method is to encode the state of the machine as a fundamental data type (usually integral) along with a number of symbolic constants which give meaning to the encodings. ● We need to encode: ● Current state ● The States ● Transitions between states ● Events and Actions

8 Show Time Adjust Hours Adjust Minutes Change Mode Set/increment hours Set/increment minutes State Machine Example ● The timeless digital watch:

9 // DigitalWatch.java public class DigitalWatch { public static final int SHOW_TIME=0; public static final int ADJUST_HOURS=1; public static final int ADJUST_MINUTES=2; private int currentState = SHOWTIME; private int hours = 0; private int minutes = 0; public void changeMode() { switch(currentState) { case SHOW_TIME:currentState = ADJUST_HOURS; break; case ADJUST_HOURS: currentState = ADJUST_MINUTES; break; case ADJUST_MINUTES:currentState = SHOW_TIME; break; } public void set() { switch(currentState) { case SHOW_TIME: // Do nothing in this state break; case ADJUST_HOURS:incrementHours(); break; case ADJUST_MINUTES: incrementMinutes(); break; }

10 private void incrementHours() { hours++; if (hours>23) hours = 0; } private void incrementMinutes() { minutes++; if (minutes>59) minutes = 0; }

11 State Design Pattern ● There is a design pattern which uses polymorphism to encode state ● The State Design pattern ● There is also a refactoring which refers to the state design pattern: ● Replace type-code with State ● General Form: ● State example: Digital Watch. AbstractState + actions ConcreteState2 + actions ConcreteState1 + actions ConcreteState3 + actions Client + actions

12 More State Examples ● Transaction Processing ● Summer 04 CPSC 457 assignment ● Stateful protocol ● SMTP (Simple mail transport protocol)


Download ppt "State ● What is state? ABCDE. State and Data Structures ABCDE A B C D E ● Data structures encode state. In doing so, they introduce state ● State of data."

Similar presentations


Ads by Google