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

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Spring 2007NOEA - Computer Science1 Design Patterns Regular Expressions (Regular Languages) State Machines Implementation of State Machine State Pattern.
Composite: Shapes Organisation State Observer Design Patterns.
Abstraction and Modular Reasoning for the Verification of Software Corina Pasareanu NASA Ames Research Center.
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 12Slide 1 Software Design l Objectives To explain how a software design may be represented.
From Cooper & Torczon1 The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source language?
Written by: Dr. JJ Shepherd
Lexical Analysis (4.2) Programming Languages Hiram College Ellen Walker.
Design Patterns Examples of smart use of inheritance and polymorphism: Composite Pattern State Pattern FEN 2014UCN Teknologi/act2learn1.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
7M701 1 Software Engineering Object-oriented Design Sommerville, Ian (2001) Software Engineering, 6 th edition: Chapter 12 )
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Component-Level Design
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
Programming Languages and Paradigms Object-Oriented Programming.
Eclipse – making OOP Easy
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Polymorphism Lecture-10. Print A Cheque A Report A Photograph PrintCheque() PrintReport() PrintPhoto() Printing.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Introduction to Object Oriented Programming CMSC 331.
Sample Application Multi Layered Architecture (n-tier): –Graphical User Interface (GUI): Forms, components, controls The Visual Designer in Visual Studio.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Prepared by: Elsy Torres Shajida Berry Siobhan Westby.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Opening Input/Output Files void openFiles ( ifstream& infile, ofstream& outfile) { char inFileName[40]; char outFileName[40]; cout
JAVA COURSE LESSON2 BY OMPUTER ENGINEEING ASSOCIATION.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Object-Oriented Programming Chapter Chapter
CIS 199 Final Review. New Material Structures  Value type  NOT a reference type!  Used to encapsulate small groups of related variables.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
ISBN Object-Oriented Programming Chapter Chapter
Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.
Decorator Design Pattern Phillip Shin. Overview Problem Solution Example Key points.
CIS 200 Test 01 Review. Built-In Types Properties  Exposed “Variables” or accessible values of an object  Can have access controlled via scope modifiers.
Written by: Dr. JJ Shepherd
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Inheritance and Polymorphism
Fusion Design Overview Object Interaction Graph Visibility Graph Class Descriptions Inheritance Graphs Fusion: Design The overall goal of Design is to.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
LECTURE 5 Scanning. SYNTAX ANALYSIS We know from our previous lectures that the process of verifying the syntax of the program is performed in two stages:
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.
The State Design Pattern A behavioral design pattern. Shivraj Persaud
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Finite-State Machines (FSMs)
Inheritance and Polymorphism
CS 153: Concepts of Compiler Design October 17 Class Meeting
Agenda Warmup AP Exam Review: Litvin A2
OOP What is problem? Solution? OOP
State pattern – A logical ‘Finite State Machine’
Finite-State Machines (FSMs)
Object Oriented Analysis and Design
State Design Pattern 1.
Advanced Programming in Java
Subtype Substitution Principle
Chapter 11 Class Inheritance
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

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

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

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

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

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!

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

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

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.

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

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

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)

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.

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

Simulating printer controlling (From: Autumn 2012UCN T&B - IT/Computer Science14

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

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