Both are different pattern usage.
1) State Pattern: All people know about the state flow diagram. how it's work based on the condition it will go one state to another state.
State Pattern means flow of states.
I am giving below examples, it will clear when to go for state pattern and when to go for strategy pattern.
public abstract class Sort {
public abstract void sortAlgorithm();
}
public class MergeSort extends Sort {
public void sortAlgorithm(){
System.out.println("MergeSorting...");
}
}
public class QuickSort extends Sort {
public void sortAlgorithm(){
System.out.println("QuickSorting...");
}
}
public class BubbleSort extends Sort{
public void sortAlgorithm(){
System.out.println("BubbleSorting...");
}
}
In the above example, we have Sort abstract class is there BubbleSort,MergeSort,QuickSort are extending that Sort.
StatePattern: My scenario is if(i==1) then BubbleSort state,if(i==2) then MergeSort state,if(i==3) then QuickSort state. my flow of states are defined like that.
public class Context {
private Sort state;
private Sort quickSort;
private Sort mergeSort;
private Sort bubbleSort;
public Context(){
quickSort = new QuickSort();
mergeSort = new MergeSort();
bubbleSort = new BubbleSort();
}
public void startWorkflow(int i){
if(i==1){
state = bubbleSort;
state.sortAlgorithm();
i=2;
}
if(i==2){
state = mergeSort;
state.sortAlgorithm();
i=3;
}
if(i==3){
state = quickSort;
state.sortAlgorithm();
}
}
}
public class StateTest {
public static void main(
String args[]){
Context ctx = new Context();
ctx.startWorkflow(1);
}
}
State Patter:"Allow an object to alter its behavior when its internal state changes",
Explanation: Here in this example Context Internal state is Sort state, based on the if condition state is getting changes, Context object behaviour (startWorkflow) changes means getting different outputs.
Output:
/* output
BubbleSorting...
MergeSorting...
QuickSorting...
Strategy Design Pattern: Scenario is I want to choose any algorithm based on the client choice.
public class Context {
private Sort state;
public Context(Sort state){
this.state = state;
}
public void sortFromContext(){
state.sortAlgorithm();
}
}
public class StrategyTest {
public static void main(String args[]){
Context ctx = new Context(new QuickSort());
ctx.sortFromContext();
}
}
output:
QuickSorting...
Strategy Principle:"Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it."
Explanation: In abstract class we define one algorithm. Each one of the specified classes would usually encapsulate an algorithm, switching between classes would switch the used algorithm and thus change the behaviour of the application.
*****Difference between State and Strategy Pattern************
In Strategy pattern, Assume one base class is there, 10 subclasses extends/implements that base class. Client will choose any one of them as strategy.Means client should explicity mention which class he is going to use.
client will any one out of n subclasses .
In State Pattern main purpose state flow like 1 -> 2 ->4->3 based on the condition. **client will not choose any of them.State Pattern main purpose flow of state it's like our state flow diagram.
Hope it's clear the concept of state vs strategy.
Regards,
Ramesh V