Design Patterns Demystified – Strategy Design Pattern

In this edition of our series of Design Patterns Demystified, we are going to discuss the strategy design pattern. So let us understand the why, what, how, and where of Strategy Design Pattern.

The Why

Let us first understand why we need this pattern. Imagine you are building an interface for vehicle simulator which has standard behaviors like startEngine(), stopEngine(), drive(), etc. for different types of vehicles, so you created a standard hierarchy of a Vehicle interface and an implementation class for Cars. This worked fine for a while, until you got a contract from an airline company that wants a behavior called fly() instead of drive() (because you don't "drive" planes, generally).