1 / 6

Some other Design Patterns…

Learn how to handle different states and state transitions in Java using guidelines, idioms, and design patterns. See examples and patterns for handling locked, unlocked, closed, open, error, awaiting combination states.

lipton
Download Presentation

Some other Design Patterns…

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. P2 — Guidelines, Idioms and Patterns Some other Design Patterns…

  2. P2 — Guidelines, Idioms and Patterns Handling States Locked unlock lock close error Awaiting combination Opened Closed open combination

  3. P2 — Guidelines, Idioms and Patterns Handling States while ((line = in.readLine()) != null) { if (line.equals("open")){ changeState(CLOSED, OPENED); } if (line.equals("close")){ changeState(OPENED, CLOSED); } if (line.equals("lock")){ changeState(CLOSED, LOCKED); } if (line.equals("unlock")){ changeState(LOCKED, AWAITING_COMBINATION); } if (line.equals("combination")){ changeState(AWAITING_COMBINATION, CLOSED); } if (line.equals("error")){ changeState(AWAITING_COMBINATION, LOCKED); } this.prompt(); }

  4. P2 — Guidelines, Idioms and Patterns State Pattern (3)

  5. P2 — Guidelines, Idioms and Patterns State Pattern Example

  6. P2 — Guidelines, Idioms and Patterns Each state is a separate object public class Opened extends State { private static Opened instance; private Opened(Controller controller){ this.controller = controller; } public static State enter(Controller controller){ if(instance == null) // lazy evaluation instance = new Opened(controller); return instance; } public void close(){ controller.changeState(Closed.enter(controller)); } }

More Related