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?
Hans Overkamp, May 21, 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.
Shmilya Golem, May 27, 2010
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
Saurabh Saxena, May 23, 2010