The strategy pattern helps extract a particular behavior out of a class. Consider for example, an application that works with several customers. We have three types of customers - standard, executive and premium. Thus, we have three subclasses of the class Customer. Also, the customers belong to several different countries. Each country has its own specifications about the transactions. How can we create a class hierarchy out of this?
If we subclass the three customer types, based on the country, we would be repeating a lot of code among them - because each country would have three classes giving the same implementation. The problem will not be solved if we do it the other way. If we first subclass based on the country and then the category, we will have a huge duplication once again.
The problem here is that different instances of any of the three Customer classes, have a different behavior based on the country. The strategy pattern helps us here, by extracting the required behavior out of the class. We can then assign it to the instance - as required.
The strategy pattern has three distincts participants.
The Strategy pattern is similar to the State Pattern - in its implementation. However, there are conceptual differences. State pattern talks about a single instance of the class that changes its behavior based on the state. However, Strategy pattern talks about statically assigning a behavior to the instance.
The State pattern allows some coupling between the state and the context. The method names and parameters and return types can be defined by the state interface and the context. But the strategy pattern is based on a simple execute() method.