Difference between Strategy and State

 
100   96.6
May 21, 2010
 
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?



90   96.5
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:

http://www.c-sharpcorner.com/UploadFile/rmcochran/strategy_state01172007114905AM/strategy_state.aspx

and

http://c2.com/cgi/wiki?StrategyPattern

- Saurabh Saxena


70   95.9
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.