The strategy behavioral pattern allows you to define multiple ways to perform some action and then select the best approach.

This pattern has some similarities to the previous state design pattern. Both patterns describe how to change behavior. But the state design pattern is more focused on changing the behavior when some internal state changes causing the program to go into different modes. While the strategy pattern is more about exposing choices that can be selected based on what’s best.

A good example of this pattern is when an adventure game allows users to select their desired difficulty level before starting a new game. Each difficulty level performs the same basic task of determining how many and how powerful the various opponents will be. The rest of the game doesn’t need to worry about which difficulty strategy was selected. And each strategy is interchangeable because the user can select which one to use. Some games even let the user change the difficulty level after the game has started.

You could think of something like this as a state, especially for these user controlled strategies, but the overall idea is different enough to warrant its own design pattern. You probably wouldn’t think of changing the difficulty level as if it was a mode that can be changed like a drawing program would change the current tool in use or like how the adventure hero gets in and out of a boat. A difficulty level is more pervasive and controls how things are done throughout the game. It forms a broader strategy.


What's on your mind?
On a scale of 0 to 10, how likely are you to refer us to friends?