1 / 25

Design Patterns

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:

tudor
Download Presentation

Design Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Regular Expressions Definition: • 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,…. NOEA - Computer Science

  3. Regular Expressions • Names for regular expressions:It can be helpful to give names to long regular expressionsL( (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 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 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 )*) NOEA - Computer Science

  6. Exercises • 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). • Try to write a regular expression that defines valid email addresses. See RegExDemo.zip and jsjsgrpregexpsyntax[1].htm 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. NOEA - Computer Science

  8. Examples End state If the DFA stops in a state that is not an end state, then input is not valid NOEA - Computer Science

  9. NOEA - Computer Science

  10. DFA defining Integers 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 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 NOEA - Computer Science

  12. Exercise • Construct a DFA that recognises identifiers 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) NOEA - Computer Science

  14. State Pattern Implements the loop that gets next state and calls any operations connected current state 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. 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! NOEA - Computer Science

  17. Signed Integer Recogniser 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 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. 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 NOEA - Computer Science

  21. Exercises • Do this exercise. • Implement the DFA that recognises identifiers using the State Pattern NOEA - Computer Science

  22. Observer Pattern View source 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. 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 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 NOEA - Computer Science

More Related