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

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

Embed Size (px)

Citation preview

Autumn 2012 UCN T&B - IT/Computer Science 1

State Pattern

Implementation of State Machines

State Pattern

Autumn 2012 UCN T&B - IT/Computer Science 2

State machine defining Integers

Autumn 2012 UCN T&B - IT/Computer Science 3

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 toset current input to next symbol in the input sequence

elseerror:= true;

endif;endwhile;if(error) report “error in input”; else report “input ok” endif

Let’s tryinput = +123

Autumn 2012 UCN T&B - IT/Computer Science 4

Implementations of State machines

• Choose an appropriate data structure to represent states:– A List or– A Dictionary– A …?

inputstate

2

states

n-1i0

s1

• Find a way to implement the transition function:– A matrix perhaps

Autumn 2012 UCN T&B - IT/Computer Science 5

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!

Autumn 2012 UCN T&B - IT/Computer Science 6

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– …..

Autumn 2012 UCN T&B - IT/Computer Science 7

State Pattern

Implements the loop that gets next state and calls

any operations connected to current state

Autumn 2012 UCN T&B - IT/Computer Science 8

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.

Autumn 2012 UCN T&B - IT/Computer Science 9

Signed Integer Recogniser

Autumn 2012 UCN T&B - IT/Computer Science 10

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 tryinput = +123

Autumn 2012 UCN T&B - IT/Computer Science 11

Discussion of the Implementation• The structure is hard-coded in the

state classes• This makes change difficult (many

classes much be changed)

Autumn 2012 UCN T&B - IT/Computer Science 12

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.

Autumn 2012 UCN T&B - IT/Computer Science 13

An alternative implementation• The Scanner class set up

the machine.

View the C# Code

Simulating printer controlling(From: http://www.go4expert.com/forums/showthread.php?t=5127)

Autumn 2012 UCN T&B - IT/Computer Science 14

Class Diagramme

Autumn 2012 UCN T&B - IT/Computer Science 15

Note how the design reflects the structure of the state machine

View source

Autumn 2012 UCN T&B - IT/Computer Science 16