CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 1 of 6 · 1 min read

Why this page comes third

In this module you will

  • Recognise an object whose behaviour depends on a state, such as a booking or a vending machine.
  • Write a state machine with if or switch and see where it breaks down.
  • Rewrite it with the State pattern, one class per state.
  • Make illegal moves fail clearly, with one default.
  • Tell State from Strategy, and choose between State classes and enums.

Strategy chose one algorithm from outside. State changes the algorithm from inside, as the object moves through its life. Many case studies in Levels 6 and 7 (vending machine, ATM, elevator, booking systems) are state machines, so this page prepares for them. The sources are "Design Patterns" (GoF) chapter 5, "Head First Design Patterns" (HF) chapter 10, and "Effective Java" (EJ) Item 34. Code was compiled and run before it was written here. Numbers marked "made up" only show the idea.