the state pattern joshua hertz nik reiman. roadmap what is the state pattern what is the state...

15
The State Pattern The State Pattern Joshua Hertz Joshua Hertz Nik Reiman Nik Reiman

Post on 20-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 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

The State PatternThe State Pattern

Joshua HertzJoshua Hertz

Nik ReimanNik Reiman

Page 2: 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

RoadmapRoadmap

What is the State PatternWhat is the State Pattern Modeling StatesModeling States Setup of the State PatternSetup of the State Pattern State DiagramState Diagram UML DiagramUML Diagram OozinozOozinoz

Page 3: 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

What is the State PatternWhat is the State Pattern

From MetskerFrom Metsker The intent … is to distribute state The intent … is to distribute state

specific logic across classes that specific logic across classes that represent an object’s state. represent an object’s state.

From the Gang of FourFrom the Gang of Four Allow an object to alter its Allow an object to alter its

behavior when its internal state behavior when its internal state changes. The object will appear changes. The object will appear to change its class.to change its class.

Page 4: 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

Modeling StatesModeling States

Can require complex cascading Can require complex cascading if statementsif statements

Have to adjust logic when state Have to adjust logic when state changeschanges

Three fundamental parts to this Three fundamental parts to this patternpattern

Page 5: 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

Setup of the State PatternSetup of the State Pattern

Interface which other classes Interface which other classes interact withinteract with

Class which maintains the Class which maintains the current statecurrent state

Individual classes for each Individual classes for each respective staterespective state

Page 6: 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

State DiagramState Diagram

Page 7: 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

UML DiagramUML Diagram

Page 8: 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

Door_2 ClassDoor_2 Classpackage com.oozinoz.carousel;package com.oozinoz.carousel;public class Door_2 extends Observable {public class Door_2 extends Observable {

public final DoorState CLOSED = new public final DoorState CLOSED = new DoorClosed(this);DoorClosed(this);

public final DoorState OPENING = new public final DoorState OPENING = new DoorClosed(this);DoorClosed(this);

public final DoorState OPEN = new public final DoorState OPEN = new DoorClosed(this);DoorClosed(this);public final DoorState CLOSING = new public final DoorState CLOSING = new

DoorClosed(this);DoorClosed(this);public final DoorState STAYOPEN = new public final DoorState STAYOPEN = new

DoorClosed(this);DoorClosed(this);////private DoorState = CLOSED;private DoorState = CLOSED;// …// …

}}

Page 9: 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

Door State ClassDoor State Classpublic abstract class DoorState {public abstract class DoorState {

protected Door_2 door;protected Door_2 door;public DoorState(Door_2 door) {public DoorState(Door_2 door) {

this.door = door;this.door = door;}}public abstract void click();public abstract void click();public void complete(){public void complete(){}}public String status(){public String status(){

String s = getClass().getName();String s = getClass().getName();return s.substring(s.lastIndexOfreturn s.substring(s.lastIndexOf

(‘.’ + 1);(‘.’ + 1);}}public void timeout(){public void timeout(){}}

}}

Page 10: 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

package com.oozinoz.carouselpackage com.oozinoz.carouselpublic class Door_2 extends Observable{public class Door_2 extends Observable{

// … (DoorState variables)// … (DoorState variables)public void click(){public void click(){

state.click();state.click();}}public void complete(){public void complete(){

state.complete();state.complete();}}protected void setState(DoorState state){protected void setState(DoorState state){

this.state = state;this.state = state;setChanged();setChanged();notifyObservers();notifyObservers();

}}public String status(){public String status(){

return state.status();return state.status();}}public void timeout(){public void timeout(){

state.timeout();state.timeout();}}

} }

Page 11: 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

package com.oozinoz.carousel;package com.oozinoz.carousel;

Public class DoorOpen extends Public class DoorOpen extends DoorState{DoorState{

public DoorOpen(Door_2 door){public DoorOpen(Door_2 door){

super(door);super(door);

}}

public void click(){public void click(){

door.setState(door.STAYOPEN);door.setState(door.STAYOPEN);

}}

public void timeout(){public void timeout(){

door.setState(door.CLOSING);door.setState(door.CLOSING);

}}

}}

Page 12: 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

Challenge-o-rama!Challenge-o-rama!

Write code for DoorClosing.javaWrite code for DoorClosing.java

Page 13: 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

Challenge-o-rama answerChallenge-o-rama answer

package com.oozinoz.carousel;package com.oozinoz.carousel;

public class DoorClosing extends DoorState 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);

}}

}}

Page 14: 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

Challenge-o-rama II: The Challenge-o-rama II: The SequelSequel Complete this diagram to show a design Complete this diagram to show a design

that moves the door states to an interface.that moves the door states to an interface.

Page 15: 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

Challenge-o-rama II: The Challenge-o-rama II: The AnswerAnswer