Design Patterns interview question · Most asked

What is the difference between the Strategy and State patterns?

Summary

Both delegate to an interchangeable object, but in Strategy the client chooses the algorithm and strategies don't know each other, while in State the state objects themselves trigger transitions to other states as the object's lifecycle progresses.

Longer Answer

Strategy answers "how should this be done?" (shipping cost: standard, express); the choice is usually made once from configuration or user input. State answers "what is allowed right now?" (an order that is pending, then paid, then shipped); behaviour changes as the object moves through states, and the states know their successors.

Their class diagrams look almost identical; the difference is who changes the delegate and why.

Key Points

  • Same structure, different intent.
  • Strategy: chosen by the client, independent.
  • State: transitions driven by states.
  • Strategy = how; State = what's allowed now.

Deep dive: Design Patterns, Explained Simply: Choosing the Right Pattern

Explanation

What interviewers look for

Clear intent difference with an example of each.

More most-asked Design Patterns questions

← All Design Patterns questions