Download presentation
Presentation is loading. Please wait.
1
The State Pattern Joshua Hertz Nik Reiman
2
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
3
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.
4
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
5
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
6
State Diagram
7
UML Diagram
8
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; // … }
9
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(){ }}
10
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();}}
11
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);}}
12
Challenge-o-rama! Write code for DoorClosing.java Write code for DoorClosing.java
13
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); }}
14
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.
15
Challenge-o-rama II: The Answer
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.