Dofactory.com
Dofactory.com
 Back to list
Views:   21.3K
Replies:  2
Archived

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
Reply 1
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
Reply 2
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

Saurabh Saxena, May 23, 2010
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.