What are the differences between the Strategy design pattern and the State design pattern? I was going through quite a few articles on the web but could not make out the dif
State comes with a little bit dependencies within the state derived classes: like one state knows about other states coming after it. For example, Summer comes after winter for any season state, or Delivery state after the Deposit state for shopping.
On the other hand, Strategy has no dependencies like these. Here, any kind of state can be initialized based on the program/product type.
Both patterns delegate to a base class that has several derivative, but it's only in the State pattern that these derivative classes hold a reference back to context class.
Another way to look at it is that the Strategy pattern is a simpler version of the State pattern; a sub-pattern, if you like. It really depends if you want the derived states to hold references back to the context or not (i.e: do you want them to call methods on the context).
For more info: Robert C Martin (& Micah Martin) answer this in their book, "Agile Principles, Patterns and Practices in C#". (http://www.amazon.com/Agile-Principles-Patterns-Practices-C/dp/0131857258)
Consider an IVR (Interactive Voice Response) system handling customer calls. You may want to program it to handle customers on:
To handle this situation you can use a State Pattern.
This process of connecting a customer to a support executive can itself be implemented using a Strategy Pattern where the executives are picked based on either of:
The strategy pattern decides on 'how' to perform some action and state pattern decides on 'when' to perform them.
Honestly, the two patterns are pretty similar in practice, and the defining difference between them tends to vary depending on who you ask. Some popular choices are:
A "classic" implementation would match either State or Strategy for every item on the list, but you do run across hybrids that have mixes of both. Whether a particular one is more State-y or Strategy-y is ultimately a subjective question.
When you have a project which can be divided into 2 tasks:
task 1: you can use one of two different algorithms to accomplish: alg1, alg2
task 2: you can use one of three different algorithms to accomplish: alg3, alg4, alg5
alg1 and alg2 are interchangeable; alg3, alg4 and alg5 are interchangeable.
Choosing which algorithm to perform in task 1 and task 2 depends on states:
state 1: you need alg1 in task 1 and alg3 in task 2
state 2: you need alg2 in task 1 and alg5 in task 2
You context can change state object from state 1 to state 2. Then your task would be accomplished by alg2 and alg5, instead of alg1 and alg3.
You can add more interchangeable algorithms for task 1 or task 2. This is strategy pattern.
You can have more states with different combination of algorithms in task 1 and task 2. State pattern allows you to switch from one state to another and perform different combination of algorithms.
'Strategy' is only an algorithm that you can change it in different circumstances upon your need, and it processes something for you. Ex. you can choose how compress a file. zip or rar ... in a method.
But 'State' CAN change all your object behaviour, when it changes, Even it can change other fields... that's why it has a reference to its owner. You should notice that changing an object field can change object behaviour at all. Ex. when you change State0 to State1 in obj, you change an integer to 10.. so when we call obj.f0() that do some calculation and use that integer, it affects the result.