This article is a follow-up to my previous article, "Patterns 101: The Factory Method". These articles are aimed at the programmer who is more comfortable with a simple procedural approach to coding and is trying to understand the benefit of object oriented programming and applying design patterns. I try to clarify the mechanics behind design patterns and explain why you would want to use them in your work. I believe a good way to clarify the concept of design patterns is to take a simple problem; solve it with a familiar, procedural approach; and then modify and redesign the solution using some basic design patterns. I've received encouraging feedback from design pattern newcomers on the effectiveness of this approach. I hope the light bulbs continue to go on.
The focus of this article will be the Strategy pattern. I chose the Strategy pattern because I find it a very useful pattern for implementing core development constructs such as business rules and other common algorithms. Additionally, understanding the reasons for implementing the strategy pattern reinforces the power of polymorphism and the benefits of taking an object oriented approach to design in general.
This example makes use of the Factory Method, discussed in my previous article. If you aren't comfortable with the Factory Method you should start by reading about it here.
If you are all set with the Factory Method, let's move on to our example that demonstrates the Strategy pattern.