State pattern 00:00:00 Make a prediction You will name which events are legal in each state and prevent a successful action from being accidentally repeated in the next state. A customer presses select on an idle machine, then presses the same button after inserting credit. Should that identical event have the same outcome twice? No. Idle must refuse; the credited state can validate stock and dispense. After dispensing, move state so a second press cannot reuse the same credit. 00:00:32 Asha and the machine with moods A toy vending machine dozes until a coin wakes it. During dispensing its slot returns an extra coin. Once the can and change appear it goes back to sleep. The button did not change; the machine's own history did. Model the current state and legal transitions before spreading checks across controls. Keep state that survives transitions, such as the current balance, with an owner that actually preserves it. The State pattern is a behavioral design pattern. An object, the context, holds a reference to one state object and forwards its events to it. Replace the state object, and the same call does something else. Without it, each method begins with the question "which state am I in?". The question is copied into every method, the copies drift apart, and combinations that make no sense become possible. 00:01:29 Reject an invalid vending action An idle vending machine receives a product selection before any credit. It must refuse without dispensing. After a coin arrives the machine enters a state where selection may proceed. That state verifies stock and credit, then records the transition. After dispensing, the same select action should no longer dispense a second item. Trace the event in each state rather than scattering conditionals across the buttons. Press dispense before inserting money. The idle state rejects it and the stock count stays unchanged. Pay and press again, then repeat the button immediately. After the successful vend the state must change, or the second press might dispense an extra item for the same payment. 00:02:14 Use a table when it is clearer A ticket can be open, assigned or closed. If each transition is simple and there are few states, an explicit transition table may be easier to read than one class per state. Introduce state objects when behaviour within those states becomes substantial. Follow a support ticket through assignment and closure. A second close can be documented as a harmless repeat or a refusal. A table with a row per current state makes that choice visible. Shared transition side effects still need one owner. Two threads selecting and cancelling can race even with well-named state classes. The transition must have one serialized owner when calls overlap. For a small finite machine with simple actions, an explicit transition table can be clearer than one class per state. A TCP connection behaves differently before it is established, while it is exchanging data and while it is closing. The same event can have different legal effects in those states. You can implement an explicit transition table without requiring a separate class for every state; the important issue is where the permitted transitions are recorded. 00:03:25 Try an interview follow-up How do you make a pay event safe against repeats? The caller creates an id per intention. State, log line and id are written in one transaction with a unique key. A repeat returns the stored result. The same id with other content is refused. Refusals are not remembered. A parcel changes from packed to dispatched to delivered. A cancellation request arrives after delivery. Ask the current state to decide whether the transition is legal. Test that a rejection leaves both the state and delivery history intact. A state name alone never enforces a transition. 00:04:06 What to take with you Draw the states and the events, and answer each cell of the table. Context forwards, states decide, one method writes the state field. Data that outlives a state lives in the context. Refuse completely: a refused coin is returned. Guards ask, entry and exit belong to the state. A new state is one class and the lines that lead to it, a new event touches all states.