StatePattern Canvas example

Hello , I have a doubt to clarify the Canvas and the tools example given by Mosh looks like strategy pattern ? Correct me if I am wrong. There is no state transitions happening in the pattern and the states are set from outside . So is this usecase for strategy pattern ?

1 Like

If you are basically wondering about the difference between the two patterns, the first few answers over at StackOverflow to this question are very useful: What is the difference between Strategy design pattern and State design pattern? - Stack Overflow

If you are wondering specifically about the Canvas / Tools example - I do believe it falls more under the State pattern since it is about changing the behavior of the canvas based on the selected tool. Implementation wise they look very similar.

In a Strategy pattern, we would mostly be trying to do the same “goal”, but selecting between different strategies to achieve that goal. A common example is sorting since there are tradeoffs between sorting algorithms and it could be wise to select between those tradeoffs even though your “goal” is consistent: sort the supplied data. So the “strategies” would be things like “quicksort” or “mergesort” or “bubblesort”.

I hope that helps anyone else who ends up wondering the same question.