Tuesday, August 31, 2010

PatternCraft - State Pattern

PatternCraft is a series of video tutorials that use StarCraft references to teach Design Patterns.

This video is the second in the series and covers the State Pattern. You can watch it in the player below or just download it here to watch however you'd like:


Other PatternCraft tutorials:
Mediator Pattern
Visitor Pattern
Memento Pattern
Command Pattern
Strategy Pattern

Source is here: http://github.com/johnlindquist/johnlindquist.com/tree/master/StatePatternDemo/src/

29 comments:

  1. I really enjoyed this video! Thanks for the clear comparison with the Strategy pattern at the end. :)

    ReplyDelete
  2. Excellent, like the previous tut

    ReplyDelete
  3. Another great vid.
    Thanks John.

    ReplyDelete
  4. jacksondunstan6:46 PM

    Great video! The only thing that bothers me about this pattern is that it forced you to make a lot of the SeigeTank public. How would you recommend avoiding this?

    ReplyDelete
  5. johnlindquist12:12 AM

    The simplest (and arguably best) answer is to just scope anything you're concerned about to "internal" namespace and keep your context/statemachine and states in the same package.

    ReplyDelete
  6. Nicolas Delfino6:47 AM

    Great video! Keep´em comming!
    /Nicolas D

    ReplyDelete
  7. Steve6:52 PM

    Thanks for the great tutorial, John!
    I don't see the reason to move the attack() and move() methods to the states, though. It seems like that breaks the DRY principle. I'm curious to here why you made that choice, and I'm looking forward to the next video!

    ReplyDelete
  8. johnlindquist9:18 PM

    This was a really small example. Imagine scaling it up to 50 states all with different types of attack and then your manager telling you to change how attack works in state 37. It may get repetitive, but the value of the organization and flexibility pays off.

    That being said, you should never make anything any more complex than it needs to be :)

    ReplyDelete
  9. Best pattern series ever!!! Thanks, man!

    ReplyDelete
  10. Smaira12:00 AM

    Brilliant tutorials. Thank you for these awesome videos. Keep them coming please!!

    ReplyDelete
  11. Bakersemail10:49 PM

    good until state handled move :-(
    imo it goes beyond what it is supposed to achieve.

    ReplyDelete
  12. johnlindquist12:12 AM

    Please elaborate...

    ReplyDelete
  13. kingundkong2:57 AM

    thanks a lot! nice movie!
    finally i got rid of my if/else mess :)

    ReplyDelete
  14. Aaron6:45 PM

    What happens if you have assets on the siege tank, such as guns and lights, that show up when in siege mode? Would that signify a need for a nested State Pattern such as LightState and GunState inside of SeighTank? my applyState method is getting packed with isGunShowing || isLightsOn ... It seems like a lot of work for toggling an animation on and off. What do you recommend?

    ReplyDelete
  15. Aaron6:58 PM

    I guess all that would go inside of the toTankState() methods

    ReplyDelete
  16. Aaron8:23 PM

    I'm confused. I've made it all the way through this tutorial and it didn't fix my need for an extensive if else statement. my applyState is filled up with showing and hiding different graphics for each state. In your example you're simply changing the color by running it through a little function, but if each state has a specific animation into a state, like morphing from the movable tank into a stationary turret, where would you handle removing the tank treads and showing the big cannon?

    ReplyDelete
  17. johnlindquist9:25 PM

    If transitioning from TankMode to SiegeMode, you'll have to implement the transition from Tank->Siege in TankMode.toSiegeMode(). You'll have to expose the tires/turret/whatever so that the states can access them.

    And yes, working with visual transitions between many states purely in code gets very verbose. It's the nature of the beast unfortunately.

    ReplyDelete
  18. Aaron9:52 PM

    Thanks. That makes sense. When do you draw the line between too many if else statements and the need for a pattern? Do you spot that you'll need the pattern right off the bat, and whip together everything without back tracking? My brain's not conditioned to think in design patterns yet :(

    ReplyDelete
  19. johnlindquist9:57 PM

    You'll reach the point where you start planning out what all your classes will be in your mind before you even write any code. Practice, practice, practice :)

    ReplyDelete
  20. Thanks for the help. I really appreciate it.

    ReplyDelete
  21. Thanks for these great tutorials! I often find myself in a scenario where my application has to handle transitioning between different views (I guess most UI-based apps work this way) and I'm always struggling with a nice and clean way to handle these transitions. In essence the views have methods for transitioning in/out and events/signals to tell when they're done transitioning*.

    But it's always hard to do this in a framework setting where you'd like a centralized transition manager to manage where to go from where. Previously I used PureMVC for most of my projects and there was an FSM-plugin for that. In my latest project I've been using RobotLegs and that too has an FSM-plugin created by Joel Hooks (in fact it's a port from the PMVC one). Problem is: I can't figure out how to use them. Even when reading through the example code. In the past I've hand-rolled my own kind of state machine, but I don't think it's flexible enough.

    I'd really like to understand the connection between an FSM and the State pattern (if there is any) and how to use an FSM in a view-based app with visual transitions, if that is indeed a good idea at all?

    So if you could just do a video tutorial on this I'd be grateful ;)
    Seriously though, keep up the excellent work. Love it!

    * Maybe this should be called something else since the view itself doesn't actually handle the transition from it to another view...

    ReplyDelete
  22. [...] comments PatternCraft – Visitor Pattern | johnlindquist.com on PatternCraft – State PatternPatternCraft – Memento Pattern | johnlindquist.com on PatternCraft – Strategy [...]

    ReplyDelete
  23. [...] PatternCraft tutorials: Mediator Pattern Memento Pattern Command Pattern State Pattern Strategy Pattern var flashvars = { width: "720", height: "586", autostart: "false", repeat: [...]

    ReplyDelete
  24. [...] PatternCraft tutorials: Mediator Pattern Visitor Pattern Command Pattern State Pattern Strategy [...]

    ReplyDelete
  25. [...] on PatternCraft – Memento PatternPatternCraft – Memento Pattern | johnlindquist.com on PatternCraft – State PatternPatternCraft – Visitor Pattern | johnlindquist.com on PatternCraft – Strategy [...]

    ReplyDelete
  26. hi John first a big thank you for your incredible jobs on patterncraft. you have the skills to turn rocket science into evidence.I have a little question . it's quite hard for me to see the difference(s) between the state pattern and the strategy pattern. They are pretty the same. I'm actually trying to experiment (as a drill) those things in a Slideshow. this slideshow would have different behaviors by clicking item on a combobox(bit101). The choices could be Coverflow , Caroussel , Grid . all thoose choice have to be supported by Papervision( with Plane Objects). Am i in a state or strategy configuration or as my little finger told me it's both of them . could you please give me some advices.
    I apologize in advance for my poor english actually i'm French.
    Olivier

    ReplyDelete
  27. hi John first a big thank you for your incredible jobs on patterncraft. you have the skills to turn rocket science into evidence.I have a little question . it's quite hard for me to see the difference(s) between the state pattern and the strategy pattern. They are pretty the same. I'm actually trying to experiment (as a drill) those things in a Slideshow. this slideshow would have different behaviors by clicking item on a combobox(bit101). The choices could be Coverflow , Caroussel , Grid . all thoose choice have to be supported by Papervision( with Plane Objects). Am i in a state or strategy configuration or as my little finger told me it's both of them . could you please give me some advices.
    I apologize in advance for my poor english actually i'm French.
    Olivier

    ReplyDelete
  28. Hi this is Olivier again
    Oops for the Strike twice
    I just listen for the very last seconds of your video and i think I'm in a state Pattern concerning my slideshow.Actually the SlideShow has to manage itself internally.
    Many thanks
    Olivier

    ReplyDelete
  29. [...] already covered the state pattern and the strategy pattern, but many people find it difficult to distinguish between the two. I put [...]

    ReplyDelete