Strategy: Implementation and Example

Learn the Strategy design pattern by implementing an example program.

Implementing the Strategy design pattern

We’ll build an application that can play audio on both Linux and Windows operating systems. It’ll be similar to the application we built while looking at the Factory Method design pattern. This time, instead of conditionally returning an appropriate player, we’ll conditionally trigger a one-off playback.

This demonstrates the fundamental difference between the Strategy and the Factory Method design patterns because it’s common for people to confuse the two. The intention behind the Factory Method is to return a long-lived object that can then be reused. The intention behind the Strategy pattern is to execute a one-off action then and there.

Creating a console application

We’ll start with creating a console application project. Then, we’ll add the following interface, which will represent our Strategy pattern.

Get hands-on with 1200+ tech skills courses.