DSA Factory
Free Design patterns lessonsDesign patterns · Stage 2 · Behaviour and communication · Step 3

State

Letting an object change how it behaves as its state changes, by moving each state's behaviour into its own class. About 12 minutes.

Quick check

What is the key difference between the state pattern and the strategy pattern?

  1. AIn state, the states themselves cause the transitions: the object changes its own behaviour over time
  2. BState has no interface
  3. CState uses static methods
Show the answer

In state, the states themselves cause the transitions: the object changes its own behaviour over time. In strategy the client picks and swaps the algorithm.

Lock it in

4 questions on this idea

Short questions that apply the idea to new cases: trace it, work out a number, put steps in order. A wrong answer explains itself.

Answer them in your browserFree account, no card. Your progress is saved, and the idea comes back in a quick review later.
Sign up free to solve