Freitag Software
The Joy of Writing Software
  • Home
    • Links
  • My Software
    • JavaScript Programs >
      • Dots
      • The Code Cracker
      • The Deserted Ship
      • Flying Text
    • Java Programs >
      • Dot Animation
      • Operation Rescue >
        • Mazes for Programmers
      • Battleship!
      • Blackjack
      • Draw Poker
      • State Pattern Demo
      • Falling Blocks
    • Android Programs >
      • The Oracle
      • RPSLS
      • Gemini Falcon: Asteroid Miner
      • Gemini Falcon: All Boxed In >
        • Gemini Falcon >
          • Privacy Policy
          • A Game Oddity
      • Ay Caramba
      • Dots vs Dots
      • Ants vs Ants
    • Twine Stories
  • Random Thoughts
  • Book Reviews
  • Teaching
    • Real-Life Stories
    • Flying Text
  • About
    • Contact

State Pattern Demo

As an exercise I created a little program to illustrate how the State Pattern could be implemented with javaFX.  
​
Picture
The program contains 2 nested State Machines. An AppMachine that is the high-level controller for the titleScreen and information screen. Other screens that might be used at this level are high score screens, preferences, and a copyright or contact screen. 

The GameMachine has 3 states: playing the game, a victory screen and an defeat screen. 

The beauty of the State Pattern is that you can add and move between new states easily. 

The State Pattern used in this demonstration program is based on the State Pattern chapter in Head First Design Patterns. 

Here is the jar file with the game. Just copy it to your computer and run it.  
Note: You will need Java on your PC to run this.
statepatterndemoprogram.jar
File Size: 41 kb
File Type: jar
Download File

Here is the source code: 
statepatterndemoprogram.zip
File Size: 12 kb
File Type: zip
Download File

Picture
Picture
Picture
Picture
Picture
This website documents my love of programming.  I hope it is useful and entertaining for you to read. 
An old programmer learning new tricks... 
(c) 2017 David A. Freitag, all rights reserved.