design patterns

25
Spring 2007 NOEA - Computer Science 1 Design Patterns Regular Expressions (Regular Languages) State Machines Implementation of State Machine State Pattern Observer Pattern Delegates

Upload: tudor

Post on 14-Jan-2016

16 views

Category:

Documents


0 download

DESCRIPTION

Design Patterns. Regular Expressions (Regular Languages) State Machines Implementation of State Machine State Pattern Observer Pattern Delegates. Regular Expressions. Definition: Regular Expressions are strings constructed by the following rules: Alphabet: Set of symbols: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Design Patterns

Spring 2007 NOEA - Computer Science 1

Design Patterns

Regular Expressions(Regular Languages)

State MachinesImplementation of State Machine

State Pattern

Observer PatternDelegates

Page 2: Design Patterns

Spring 2007 NOEA - Computer Science 2

Regular ExpressionsDefinition:• Regular Expressions are strings constructed by the

following rules:– Alphabet: Set of symbols:

• For instance: {a,b,c} or the ASCII char-set or…

– Epsilon (ε): empty string– Selection: For instance a | b (a or b)– Iteration: For instance (a)* (0 or more ‘a’s concatenated)– Concatenation: One string followed by an other– For instance:

• ((a|b)a)* means: an a or a b followed by an a – everything repeated 0 or more times, that is: strings of the form: ε, aa, ba, aaaa, aaba, baaa, baba,….

Page 3: Design Patterns

Spring 2007 NOEA - Computer Science 3

Regular Expressions

• Names for regular expressions:It can be helpful to give names to long regular expressions

L( (0 | 1 | 2 | … | 9) (0 | 1 | 2 | … | 9)* ) = {0,1,2,3, … 10,11,…}

or we could write:digit digit*

where digit = 0 | 1 | 2 | … | 9

Page 4: Design Patterns

Spring 2007 NOEA - Computer Science 4

Extensions

– One or More repetition ‘+’L( (0 | 1 | 2 | … | 9)+) = {0, 1, 2, … 10, 11 ,12}

– Any Character ‘.’.*b.* means at least one b

– A range of Characters ‘[]’[a-zA-Z] = a | b | … | z | A | B | … | Z [abc] = a | b | c

– Any Character Not In a Given Set~(a | b | c) means not either a or b or c.

– Optional ‘?’a? means zero or one a

Page 5: Design Patterns

Spring 2007 NOEA - Computer Science 5

Examples

• Some other examples:

– Let Σ = {a,b,c}. Consider the strings that contains exactly one b.

L( (a | c)*b(a | c )* ) = {b, abc, abaca, … }

– Let Σ = {a,b,c}. Consider the strings that contains at most one b.

L((a | c)*b(a | c )* | (a | c)* ) = {ε, a, c, b, abc, abaca, … }

or alternative

L( (a | c)* (b | ε)(a | c )*)

Page 6: Design Patterns

Spring 2007 NOEA - Computer Science 6

Exercises

1. Write a regular expression that defines valid identifiers in some programming language (the only characters allowed in an identifier are letters, digits and underscore (‘_’), and it always starts with a letter).

2. Try to write a regular expression that defines valid email addresses.

See RegExDemo.zip and jsjsgrpregexpsyntax[1].htm

Page 7: Design Patterns

Spring 2007 NOEA - Computer Science 7

State Machine(Deterministic) Finite Automata

• A Deterministic Finite Automata (DFA) is a device that is able to recognise regular expressions.

• There is an one-to-one relation between a regular expression and a DFA:– Given a regular expression, there is an algorithmic

construction of corresponding DFA

• A DFA has no state from which more than one transition is possible on the same input symbol.

Page 8: Design Patterns

Spring 2007 NOEA - Computer Science 8

ExamplesEnd state

If the DFA stops in a state that is not an end state, then input is not

valid

Page 9: Design Patterns

Spring 2007 NOEA - Computer Science 9

Page 10: Design Patterns

Spring 2007 NOEA - Computer Science 10

DFA defining Integers

digit unsigned

start integer digit

sign digit eotxt

signed other end integer

other everything

other error

Page 11: Design Patterns

Spring 2007 NOEA - Computer Science 11

Scanner Loop

state:= 1; 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

sequenceelse

error:= true;endif;

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

Page 12: Design Patterns

Spring 2007 NOEA - Computer Science 12

Exercise

3. Construct a DFA that recognises identifiers

Page 13: Design Patterns

Spring 2007 NOEA - Computer Science 13

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

Regular Languages

(Expressions)

Page 14: Design Patterns

Spring 2007 NOEA - Computer Science 14

State Pattern

Implements the loop that gets next state

and calls any operations connected

current state

Page 15: Design Patterns

Spring 2007 NOEA - Computer Science 15

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.

Page 16: Design Patterns

Spring 2007 NOEA - Computer Science 16

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

• 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 parser loop uses references having the abstract class State as static type.

• Polymorphism and dynamic binding handles the rest!

Page 17: Design Patterns

Spring 2007 NOEA - Computer Science 17

Signed Integer Recogniser

Page 18: Design Patterns

Spring 2007 NOEA - Computer Science 18

OO Parser Loop 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 Code

Page 19: Design Patterns

Spring 2007 NOEA - Computer Science 19

Regular Languages (Expressions) and DFAs

• A regular language is a language which syntax can be define by a regular expression or a regular grammar. Regular languages is a subset of the context free languages (most programming language are context free).

• Regular languages don’t allow recursive constructions. Recursion can be transformed to iteration.

– This means that regular languages cannot have nested blocks as:• Expressions with nested parenthesis• begin-end –blocks (’{’-’}’) or similar constructs

• Regular expressions are important in validating input, parsing protocols etc.

Page 20: Design Patterns

Spring 2007 NOEA - Computer Science 20

RegEx in .NET

bool IsValidEmail(string strIn)

{

// Return true if strIn is in valid e-mail format.

return Regex.IsMatch(strIn,

@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.) |

(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");

}

What is defined here?

Documentation

Page 21: Design Patterns

Spring 2007 NOEA - Computer Science 21

Exercises

4. Do this exercise.

5. Implement the DFA that recognises identifiers using the State Pattern

Page 22: Design Patterns

Spring 2007 NOEA - Computer Science 22

Observer Pattern

View source

BottleState

ChangeAmount()

IObserver

NotifyMe()

IObservable

NotifyAll()Add()Remove()

0..*0..1 0..*0..1

Optimist Pessimist

Page 23: Design Patterns

Spring 2007 NOEA - Computer Science 23

Observer Pattern

• En række forskellige objekter (observers) ønsker at få at vide, når et objekt (observable eller subject) skifter tilstand.

• Subject skal tillade observers at melde sig dynamisk og skal ikke vide noget om dem.

BottleState

ChangeAmount()

IObserver

NotifyMe()

IObservable

NotifyAll()Add()Remove()

0..*0..1 0..*0..1

Optimist Pessimist

Page 24: Design Patterns

Spring 2007 NOEA - Computer Science 24

Observer Pattern

• Fx ønsker en række objekter at blive underrettet om ændringer i et andet objekt.

• Disse objekter (observers) skal implementere interfacet IObserver.

• Dvs. metoden NotifyMe, hvor deres handlinger kodes

• Subject skal holde styr på en collection af observers og kunne notificere alle objekter i collectionen (IObservable).

• Det konkrete Subject kalder NotifyAll, når der er et relevant tilstandsskift

BottleState

ChangeAmount()

IObserver

NotifyMe()

IObservable

NotifyAll()Add()Remove()

0..*0..1 0..*0..1

Optimist Pessimist

Page 25: Design Patterns

Spring 2007 NOEA - Computer Science 25

Observer Pattern

• Observer Pattern er et OO-design, som simulerer funktionspointere i fx C og højereordensfunktioner i fx Pascal

• I C# findes sprogkonstruktionen delegate, som giver nogle af de samme muligheder. – Se eksempel