Presentation is loading. Please wait.

Presentation is loading. Please wait.

Autumn 2012UCN T&B - IT/Computer Science1 State Pattern Implementation of State Machines State Pattern.

Similar presentations


Presentation on theme: "Autumn 2012UCN T&B - IT/Computer Science1 State Pattern Implementation of State Machines State Pattern."— Presentation transcript:

1 Autumn 2012UCN T&B - IT/Computer Science1 State Pattern Implementation of State Machines State Pattern

2 Autumn 2012UCN T&B - IT/Computer Science2 State machine defining Integers

3 Autumn 2012UCN T&B - IT/Computer Science3 Scanner Loop state:= start; error:= false; while(not eotxt and not error) if (exists transition from state marked with current input symbol) state:= the state that this transition leads to set current input to next symbol in the input sequence else error:= true; endif; endwhile; if(error) report “error in input”; else report “input ok” endif Let’s try input = +123

4 Autumn 2012UCN T&B - IT/Computer Science4 Implementations of State machines Choose an appropriate data structure to represent states: –A List or –A Dictionary –A …? input state 2 states n-1 i 0 s1 Find a way to implement the transition function: –A matrix perhaps

5 Autumn 2012UCN T&B - IT/Computer Science5 OO Implementation State is an object State Pattern can be applied: –abstract class State specifies one or more abstract methods: transition(-) – returns state corresponding to input symbol (or next event) action(-) – if any processing is to be done when a transition occurs (code generation, event handling etc.) each concrete state inherits from State and implements the abstract methods The scanner loop uses references having the abstract class State as static type. Polymorphism and dynamic binding handles the rest!

6 Autumn 2012UCN T&B - IT/Computer Science6 State Pattern Implements state machines (DFA) encapsulating state. Provides addition of new states without changing existing code. Examples: –Dialog box for editing parameters to a program –XML –Parsing protocols –Parser/scanner in a compiler or a browser or… –Event handling in a windows system –…..

7 Autumn 2012UCN T&B - IT/Computer Science7 State Pattern Implements the loop that gets next state and calls any operations connected to current state

8 Autumn 2012UCN T&B - IT/Computer Science8 The Classes of the Pattern Context : Defines the objects that we want maintain state information about (for instance DialogBox). This class has a reference (static type: ContextState – the abstract super class) to some concrete state (that is an object of one of the sub classes – dynamic type). ContextState: The abstract super class defining a common interface to all the concrete states. ConcreteState1,...: The sub classes to ContextState. One sub class to each state in the DFA. The key to the design is in the processEvent-method, which takes an event as input and returns the next state.

9 Autumn 2012UCN T&B - IT/Computer Science9 Signed Integer Recogniser

10 Autumn 2012UCN T&B - IT/Computer Science10 OO Scanner Loop //In class Scanner: public bool Scan(string input) { //input.Length>0 bool ok = false; int i = 0; char nextChar = input[i]; State currState = s1.Transition(nextChar); while (currState != s5 && currState != s4) { i++; if (i == input.Length) nextChar = '\0'; else nextChar = input[i]; currState = currState.Transition(nextChar); } if (currState == s5) ok = true; return ok; } View the C# Code Let’s try input = +123

11 Autumn 2012UCN T&B - IT/Computer Science11 Discussion of the Implementation The structure is hard-coded in the state classes This makes change difficult (many classes much be changed)

12 Autumn 2012UCN T&B - IT/Computer Science12 An alternative implementation The abstract class State has a collection (Dictionary) of connected states and events. That collection is inherited to all concrete states. The concrete states implement Transition(-) and the relevant transition is returned.

13 Autumn 2012UCN T&B - IT/Computer Science13 An alternative implementation The Scanner class set up the machine. View the C# Code

14 Simulating printer controlling (From: http://www.go4expert.com/forums/showthread.php?t=5127)http://www.go4expert.com/forums/showthread.php?t=5127 Autumn 2012UCN T&B - IT/Computer Science14

15 Class Diagramme Autumn 2012UCN T&B - IT/Computer Science15

16 Note how the design reflects the structure of the state machine View source Autumn 2012UCN T&B - IT/Computer Science16


Download ppt "Autumn 2012UCN T&B - IT/Computer Science1 State Pattern Implementation of State Machines State Pattern."

Similar presentations


Ads by Google