State Design Pattern

Derek Banas · 247K views · 2012 · 20:51

Open on YouTube

Software architecture & designSoftware practice, open source & careerstutorial

Why Stack Overflow recommends it

Stack Overflow users link it as a worked example of the State pattern, for instance when porting the ATM example to Dart enums or structuring state-dependent game code.

Recommended by 2 people across 2 different months, 2014 to 2022.

What it is

Derek Banas walks through the State design pattern by building an ATM machine simulation. He explains how to decide on the states, design the shared interface each state implements, and then fills in the code for each state class.

What people said

Recommends Repareman · 2014-11 · Stack Overflow · on “How can i turn the camera to left and right in unity for a sphere?”

Alright, so if I'm understanding you correctly, you want the ball to move in relation to which direction the camera is currently facing. As well as you don't want to have to hold down "Q" or "E" while you move your ball with WASD controls. So there are a few things that I'd like to mention here, so we'll take them one at a time. First off, you shouldn't have "AddForce" inside of an Update call. Update is called every frame whenever it can. It's great for input, but generally you want to call it in "FixedUpdate" instead. It provides a better response across many devices. If you leave it in update, you can get inaccurate physics results, and missed collisions. There's a lot out there on this s

Recommends Mike · 2022-03 · Stack Overflow · on “Unity C# collision detection”

You're validating input, and updating physics both in the void OnTriggerEnter method. Physics updates much differently than the regular old Update() method does. Your physics checks are already completed (I guess in this case it could be either or). By calling Input.GetButtonDown() in this method, it has to grab the exact frame that button mashed down to return true. The problem is, physics don't Update in the same way. Actually, you aren't even running a physics check this frame. That button just got completely ignored. Dang. You have to come up with a better way to structure your code to process that input. Find a way that allows you check if the player is attacking, without relying on inp

Cites Suragch · 2022-06 · Stack Overflow · on “How to apply the State Pattern with Dart enhanced enums”

Dart 2.17 now has enhanced enums. Matt Carroll mentioned in his review [LINK] that you could use the new enums to implement the State Pattern. I'm having trouble with this because I'm weak on both the State Pattern and on enhanced enums. Here is an example of the State Pattern without enums from this YouTube video [this video] that I converted to Dart: // [this video] void main() { final atmMachine = AtmMachine(); atmMachine.insertCard(); atmMachine.ejectCard(); atmMachine.insertCard(); atmMachine.insertPin(1234); atmMachine.requestCash(2000); atmMachine.insertCard(); atmMachine.insertPin(1234); } abstract class AtmState { void insertCard(); void ejectCard(); void insertPin(int pin); void requestCash(