JB Header
Strategy Design Pattern versus State Design Pattern - An Analysis
This tutorial analyses Gang of Four's Strategy Design Pattern versus State Design Pattern w.r.t to their similarities and differences.

If you want to read in-depth tutorials on the patterns themselves then you can read them here - State Pattern tutorial Read State Design Pattern Tutorial and Strategy Pattern tutorialRead Strategy Design Pattern Tutorial. Similarities between State Pattern and Strategy Pattern
  1. Class Diagrams of the Patterns: One of the biggest similarities between state pattern and strategy pattern is their class diagrams which look almost identical but for the class names. Both patterns define a base state/strategy and sub-states/sub-strategies are children of the base class. Here's the class diagrams of Strategy Design Pattern and State Design Patterns next to each other -
    Strategy Design Pattern versus State Design Pattern
  2. Both follow Open Closed Principle: State Pattern's Context is closed for modification. I.e. how the States are accessed & used is fixed. But the individual states are open i.e. more states can be added via extension. Similarly, Strategy pattern's context is closed for modification but the individual strategies' child classes are open via extension.
  3. Use of subclasses: Both State & Strategy Pattern use subclasses to change the implementation via different derived implementations of States/Strategies.
Differences between State Pattern and Strategy Pattern
  1. Intent of the Pattern: Intent or purpose of Strategy Pattern is to have a family of interchangeable algorithms which can be chosen based on the context and/or client needs. On the other hand, State Pattern's intent is to manage states of the object along with object's behavior which changes with its state.
  2. Client Awareness of the strategy/state: In a Strategy Pattern implementation the strategy chosen is client-dependent and hence the client is aware which strategy is being used. However, in State Pattern implementation client interacts with the context to act on the object but does not decide on which State to chose. The Object itself appears to change its State Class based on the interaction the Client has via the Context.
  3. Reference back to the Context: Every state in the State Pattern holds a reference back to the Context. However, each strategy does not hold the handle back to the Context in the Strategy Pattern.
  4. Relation between individual states/individual strategies: Different states in the State Pattern are related to one another, say as a successor or predecessor etc. This is because there is a flow among the states like a Finite State Machine. Strategy Pattern, however, just chooses one of the strategies from the multiple strategies available. There is no successor/predecessor relationship between strategies.
  5. How v/s What & When: Multiple strategies define multiple ways of how to do something.On the other hand, multiple states define what is to be done and based on the relationship between states when it is to be done.
Summary In this tutorial we analyzed Strategy Design Pattern versus State Design Pattern from the point of view of their similarities and differences.