3-1 State Design Pattern C Sc 335 Rick Mercer. State Design Pattern State is one of the Behavioral patterns It is similar to Strategy Allows an object.

Slides:



Advertisements
Similar presentations
UML State Machine Diagrams and Modeling
Advertisements

CE881: Mobile and Social Application Programming Simon M. Lucas Menus and Dialogs.
By : Atri Chatterjee Souvik Ghosh
Classes & Objects Computer Science I Last updated 9/30/10.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
Matt Klein 7/6/2009.  Behavioral Pattern  Intent  Allow an object to alter its behavior when its internal state changes. The object will appear to.
Design Patterns in Java Appendix D UML at a Glance Summary prepared by Kirk Scott 1.
Object Oriented System Development with VB .NET
The State Pattern Joshua Hertz Nik Reiman. Roadmap What is the State Pattern What is the State Pattern Modeling States Modeling States Setup of the State.
Design Patterns 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Visitor Matt G. Ellis. Intent Metsker: Let developers define a new operation for a hierarchy without changing the hierarchy classes. GoF: Represent an.
Model View Controller (MVC) Architecture. Terminology and History MVC evolved from Smalltalk-80 Has become a key pattern in web based applications – If.
Graphics without NGP The key to effectively using graphics in Java is understanding: –the basic components of the graphics library –the patterns that are.
1 CSE 331 The Strategy and State Patterns slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Event Handling in Java: Alternatives and Patterns Raja Sooriamurthi Information Systems Department Kelley School of Business Indiana.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
1 v1.6 08/02/2006 Overview of Eclipse Lectures 1.Overview 2.Installing and Running 3.Building and Running Java Classes 4.Refactoring 5.Debugging 6.Testing.
Implementing Design Patterns Using Java St. Louis Java Special Interest Group Eric M. Burke Object Computing, Inc. Presented on July 9, 1998 (updated July.
CSC 313 – Advanced Programming Topics. Observer Pattern Intent  Efficiently perform 1-to-many communication  Easy to respond dynamically when event(s)
CSE 331 Software Design & Implementation Dan Grossman Winter 2014 Events, Listeners, and Callbacks (Based on slides by Mike Ernst, David Notkin, Hal Perkins)
CS 210 Introduction to Design Patterns September 7 th, 2006.
 How are you going to collaborate?  How are you going to divide up work?  How are you going to make sure that changes work with other people’s code?
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
CPSC 871 John D. McGregor Module 7 Session 1 More UML.
Model View Controller (MVC) Bigger than a Pattern: It’s an Architecture Rick Mercer with help from many others 1.
Unit 4 Object-Oriented Design Patterns NameStudent Number CAI XIANGHT082182A KYAW THU LINHT082238Y LI PENGFEIHT082220L NAUNG NAUNG LATTHT082195L PLATHOTTAM.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Appendix D UML at a Glance Summary prepared by Kirk Scott 1.
CSC 313 – Advanced Programming Topics. Observer Pattern in Java  Java ♥ Observer Pattern & uses everywhere  Find pattern in JButton & ActionListener.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
1 CSC/ECE 517 Fall 2010 Lec. 3 Overview of Eclipse Lectures Lecture 2 “Lecture 0” Lecture 3 1.Overview 2.Installing and Running 3.Building and Running.
Model View Controller (MVC) Bigger than a Pattern: It’s an Architecture Rick Mercer with help from many of others 1.
OO Methodology Elaboration Iteration 2 - Design Patterns -
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
OBSERVER DESIGN PATTERN. Behavioral Patterns  Behavioral patterns are those patterns that are most specifically concerned with communication between.
Testing OO software. State Based Testing State machine: implementation-independent specification (model) of the dynamic behaviour of the system State:
State Design Pattern. Behavioral Pattern Allows object to alter its behavior when internal state changes Uses Polymorphism to define different behaviors.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
STRATEGY PATTERN. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
More DFDs Class 12.
Model View Controller (MVC) an architecture Rick Mercer with help from many of others 1.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
Slide design: Dr. Mark L. Hornick
The State Design Pattern A behavioral design pattern. Shivraj Persaud
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Observer Pattern Context:
Software Design Refinement Using Design Patterns
Sections Inheritance and Abstract Classes
Unit II-Chapter No. : 5- design Patterns
Design Patterns in Java Chapter 22 State
Module Road Map Refactoring Why Refactoring? Examples
State pattern – A logical ‘Finite State Machine’
UML Class Diagram: class Rectangle
State Design Pattern 1.
Introduction to Behavioral Patterns (1)
Introduction to Behavioral Patterns (2)
DESIGN PATTERNS : State Pattern
Design pattern Lecture 9.
Advanced ProgramMING Practices
8. Observer Pattern SE2811 Software Component Design
Advanced ProgramMING Practices
Presentation transcript:

3-1 State Design Pattern C Sc 335 Rick Mercer

State Design Pattern State is one of the Behavioral patterns It is similar to Strategy Allows an object to alter its behavior when its internal state changes The object will appear to change its class

General Form from Wikipedia, copied from Gof4 “Context” class: Represents the interface to the outside world “State” abstract class: Base class which defines the different states of the “state machine” “Derived” classes from the State class: Defines the implementation of different states

Non Computer Example MyMoodMoodState doSomething() madangryhappy doSomething() state variable Client doSomething()

Another Example from Steve Metsker's Design Patterns Java Workbook Consider the state of a carousel door in a factory Large smart rack that accepts material through a doorway and stores material according to a bar code

Behavior of this manufacturing device There is a single button to operate this door if closed, door begins opening if opening, another click begins closing once open, 2 seconds later (timeout), the door begins closing can be prevented by clicking after open state and before timeout begins Consider two ways to do this If statements State OO design pattern

Transition from one state to another This can get messy. Doing animations? read public void click() { if (state == DoorStateEnum.CLOSED) { setState(DoorStateEnum.OPENING); } else if (state == DoorStateEnum.OPENING || state == DoorStateEnum.STAYOPEN) { setState(DoorStateEnum.CLOSING); } else if (state == DoorStateEnum.OPEN) { setState(DoorStateEnum.STAYOPEN); } else if (state == DoorStateEnum.CLOSING) { setState(DoorStateEnum.OPENING); }

Alternative: The State Pattern Draw a State Diagram first A box representing one of the 5 states CLOSED OPEN… When the carousel door can respond to a button click, or an observable’s update message, draw an arrow to the next state and label the transition with click Or use the transitions complete or timeout

A UML State Diagram also a CSc 473 topic: finite state machine

State Machine characteristics Fixed set of states Closing, opening, closed, open Can only be in one state at a time Can’t be opening and closing at the same time Events are sent to the machine Button clicks, observer notification Each state has a transition Machine changes to the state that transition points

Things to do implement state Define a “context” class to present a single interface Door Define a state abstract base class DoorState Represent different “states” by extending DoorState with DoorOpen, DoorClosed, DoorClosing, … Define state-specific behavior in the appropriate subclasses public class DoorOpening extends DoorState { public void click() { door.setState(door.CLOSING); }

Things to do implement state Maintain current “state” in the “context” class private DoorState state = CLOSED; Change the state of the state machine in context and notify protected void setState(DoorState state) { this.state = state; setChanged(); notifyObservers(); }

Code reverse engineered (demo)

Some of you might use State Complex if statements determine what to do if (thisSprite == running) doRunAnimation(); else if (thisSpite == shooting) doShootingAnimination(); else if (thisSpite == noLongerAlive) doRollOverAnimation();... Or An object can be in one of several states, with different behavior in each state state.doAnimation()