1 / 15

The State Pattern

The State Pattern. Joshua Hertz Nik Reiman. Roadmap. What is the State Pattern Modeling States Setup of the State Pattern State Diagram UML Diagram Oozinoz. What is the State Pattern. From Metsker

joycesnider
Télécharger la présentation

The State Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The State Pattern Joshua Hertz Nik Reiman

  2. Roadmap • What is the State Pattern • Modeling States • Setup of the State Pattern • State Diagram • UML Diagram • Oozinoz

  3. What is the State Pattern • From Metsker • The intent … is to distribute state specific logic across classes that represent an object’s state. • 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.

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

  5. Setup of the State Pattern • Interface which other classes interact with • Class which maintains the current 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

  13. Challenge-o-rama answer package com.oozinoz.carousel; public class DoorClosing extends DoorState { public DoorClosing(Door_2 door) { super(door); } public void click() { door.setState(door.OPENING); } public.void complete() { 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.

  15. Challenge-o-rama II: The Answer

More Related