By Señora Maria del Pilar Reiman [email protected] Jackson High School : Foreign language Classes.
The State Pattern Joshua Hertz Nik Reiman. Roadmap What is the State Pattern What is the State...
-
date post
20-Dec-2015 -
Category
Documents
-
view
215 -
download
1
Transcript of The State Pattern Joshua Hertz Nik Reiman. Roadmap What is the State Pattern What is the State...
The State PatternThe State Pattern
Joshua HertzJoshua Hertz
Nik ReimanNik Reiman
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
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.
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
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
State DiagramState Diagram
UML DiagramUML Diagram
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;// …// …
}}
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(){}}
}}
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();}}
} }
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);
}}
}}
Challenge-o-rama!Challenge-o-rama!
Write code for DoorClosing.javaWrite code for DoorClosing.java
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);
}}
}}
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.
Challenge-o-rama II: The Challenge-o-rama II: The AnswerAnswer