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.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
1 Chapter 11 Introducing the Class Pages ( )
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
The Flyweight Pattern Nik Reiman Joshua Hertz. Roadmap What is the Flyweight Pattern? UML When is Flyweight Useful? How to Use the Flyweight Pattern?
Satzinger, Jackson, and Burd Object-Orieneted Analysis & Design
Visitor Matt G. Ellis. Intent Metsker: Let developers define a new operation for a hierarchy without changing the hierarchy classes. GoF: Represent an.
UML a crash course Alex Lo Brian Kiefer. Overview Classes Class Relationships Interfaces Objects States Worksheet.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Unified Modeling Language
CS 2511 Fall UML Diagram Types  2 Main Types Structure Diagrams ○ Class Diagrams ○ Component Diagrams ○ Object Diagrams Behavior Diagrams ○
Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
Inheritance using Java
Session 12 Applying the Class Diagram to the Case Study Written by Thomas A. Pender Published by Wiley Publishing, Inc. October 12, 2011 Presented by Hyewon.
Emeka Egbuonye CSPP March 02,2010 The Mediator Pattern.
Case Studies on Design Patterns Design Refinements Examples.
Abstract Factory Design Pattern making abstract things.
BUILDER, MEDIATOR, AND DECORATOR Team 2 (Eli.SE).
CS 153: Concepts of Compiler Design August 26 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
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.
Incremental Design Why incremental design? Goal of incremental design Tools for incremental design  UML diagrams  Design principles  Design patterns.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Data Structures Using C++ 2E
CSE 219 Computer Science III UML. UML Diagrams UML - Unified Modeling Language UML diagrams are used to design object-oriented software systems –represent.
Design Patterns Introduction
11. Reference Organization Packages. What is reference organization? It is a process that enables us to group classes and interfaces into isolated namespaces.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Unified Modeling Language (UML)
UML Fundamental Elements. Structural Elements Represent abstractions in our system. Elements that encapsulate the system's set of behaviors. Structural.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Construction Lecture Oo21 Gymnastics System Example Cont’d.
Design Patterns: Brief Examples
Design Patterns in Java Chapter 22 State
Unified Modeling Language
Chapter No. : 1 Introduction to Java.
Interface, Subclass, and Abstract Class Review
BTS430 Systems Analysis and Design using UML
Lecture 2: Data Types, Variables, Operators, and Expressions
State pattern – A logical ‘Finite State Machine’
CSC 205 Java Programming II
null, true, and false are also reserved.
Interface.
State Design Pattern 1.
Introduction to Behavioral Patterns (2)
Interfaces.
Jeopardy Final Jeopardy Topic 1 Topic 2 Topic 3 Topic 4 Topic 5 $100
OBJECT ORIENTED ANALYSIS AND DESIGN
User-Defined Classes and ADTs
Chapter 8 Classes User-Defined Classes and ADTs
State Pattern By the Group of Four.
By Rajanikanth B OOP Concepts By Rajanikanth B
Jeopardy Final Jeopardy Topic 1 Topic 2 Topic 3 Topic 4 Topic 5 $100
Jeopardy Final Jeopardy Topic 1 Topic 2 Topic 3 Topic 4 Topic 5 $100
Jeopardy Final Jeopardy Topic 1 Topic 2 Topic 3 Topic 4 Topic 5 $100
Jeopardy Final Jeopardy Topic 1 Topic 2 Topic 3 Topic 4 Topic 5 $100
2.1 Introduction to Object-Oriented Programming
Jeopardy Final Jeopardy Topic 1 Topic 2 Topic 3 Topic 4 Topic 5 $100
Jeopardy Final Jeopardy Topic 1 Topic 2 Topic 3 Topic 4 Topic 5 $100
Agenda Types and identifiers Practice Assignment Keywords in Java
Jeopardy Final Jeopardy Topic 1 Topic 2 Topic 3 Topic 4 Topic 5 $100
Jeopardy Final Jeopardy Topic 1 Topic 2 Topic 3 Topic 4 Topic 5 $100
Topics OOP Review Inheritance Review Abstract Classes
Proxy Pattern Definition of “Proxy”: Authority or power to act for another Original Gang of Four pattern, much used Stands in for a “real object” (similar.
Jeopardy Final Jeopardy Topic 1 Topic 2 Topic 3 Topic 4 Topic 5 $100
Lesson 3. Controlling program flow. Loops. Methods. Arrays.
DIAGRAM IT!.
Jeopardy Final Jeopardy Topic 1 Topic 2 Topic 3 Topic 4 Topic 5 $100
Presentation transcript:

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 Pattern Setup of the State Pattern State Diagram State Diagram UML Diagram UML Diagram Oozinoz Oozinoz

What is the State Pattern From Metsker From Metsker The intent … is to distribute state specific logic across classes that represent an object’s state. The intent … is to distribute state specific logic across classes that represent an object’s state. From the Gang of Four From the Gang of Four Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

Modeling States Can require complex cascading if statements Can require complex cascading if statements Have to adjust logic when state changes Have to adjust logic when state changes Three fundamental parts to this pattern Three fundamental parts to this pattern

Setup of the State Pattern Interface which other classes interact with Interface which other classes interact with Class which maintains the current state Class which maintains the current state Individual classes for each respective state Individual classes for each respective state

State Diagram

UML Diagram

Door_2 Class package com.oozinoz.carousel; public class Door_2 extends Observable { public final DoorState CLOSED = new DoorClosed(this); public final DoorState OPENING = new DoorClosed(this); public final DoorState OPEN = new DoorClosed(this); public final DoorState CLOSING = new DoorClosed(this); public final DoorState STAYOPEN = new DoorClosed(this); // private DoorState = CLOSED; // … }

Door State Class public abstract class DoorState { protected Door_2 door; public DoorState(Door_2 door) { this.door = door; } public abstract void click(); public void complete(){ } public String status(){ String s = getClass().getName(); return s.substring(s.lastIndexOf (‘.’ + 1); } public void timeout(){ }}

package com.oozinoz.carousel public class Door_2 extends Observable{ // … (DoorState variables) public void click(){ state.click();} public void complete(){ state.complete(); } protected void setState(DoorState state){ this.state = state; setChanged();notifyObservers();} public String status(){ return state.status(); } public void timeout(){ state.timeout();}}

package com.oozinoz.carousel; Public class DoorOpen extends DoorState{ public DoorOpen(Door_2 door){ super(door);} public void click(){ door.setState(door.STAYOPEN);} public void timeout(){ door.setState(door.CLOSING);}}

Challenge-o-rama! Write code for DoorClosing.java Write code for DoorClosing.java

Challenge-o-rama answer package com.oozinoz.carousel; public class DoorClosing extends DoorState { public DoorClosing(Door_2 door) { public DoorClosing(Door_2 door) { super(door); super(door); } public void click() { public void click() { door.setState(door.OPENING); door.setState(door.OPENING);} public.void complete() { public.void complete() { door.setState(door.CLOSED); door.setState(door.CLOSED); }}

Challenge-o-rama II: The Sequel Complete this diagram to show a design that moves the door states to an interface. Complete this diagram to show a design that moves the door states to an interface.

Challenge-o-rama II: The Answer