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