Home  /  Questions  /  Question

100   96.6
May 21, 2010

Difference between Strategy and State

Looking at the GoF patterns I find the similarities between State and Stategy pattern rather striking.
Both swap out polymorphic classes to modify behavior. Anyone else found the same?

What are the exact differences?

100   96.6
May 23, 2010
Hello Hans,

There are many discussions on the difference in State and Strategy Pattern because both share the same class diagram.
However, one big difference is the following:

Strategy Pattern is used when the whole algorithm is changed to another algorithm and the client is responsible for that, whereas, in State Pattern, the class itself manages the strategy based on the state.

Here are some good discussions on this topic:




- Saurabh Saxena

70   96.1
May 27, 2010
There is a similar relationship between the Strategy/State pattern pairs and the Factory Method/Abstract Factory pattern pairs.

Strategy is used in one place where objects can change; just like the Factory Method is used for creating one type of product.
State pattern holds series of objects that are changed; just like Abstract Factory is used for creating series of products.