Design Pattern Dating Profiles: Strategy Pattern

2 min readPublished July 09, 2018Updated May 02, 2022

I love design patterns. I like having a structured way to organize my code and having a shared vocabulary in which to talk about how my code is structured, but I often mix up which pattern is which. I decided to make a dating profile for each pattern as a way to remember the various patterns. This series is not meant to be a deep dive into the patterns and how to implement them (there are many posts on that topic already) but is meant to provide a high level understanding.

The first pattern I chose is the Strategy pattern. It is easily my favorite and most used of the traditional design patterns. The Strategy pattern defines an interface for an algorithm and separate classes for each implementation of the algorithm. The goal is to encapsulate what varies into its own class. Using the Strategy pattern, your program chooses which implementation to use at runtime, based on context within the program such as user preferences.

Flexibility

The Strategy pattern is a great first step to making your code more flexible and less brittle to change. The Strategy pattern minimizes the amount of code we need to change when requirements change. If we’re able to encapsulate algorithms as a class and code to the interface, then we only need to define a new class that satisfies the new requirements and our calling code doesn’t need to be touched. Not only does this save time but is safer as it can help minimize regressions.

Because of this flexibility, the Strategy pattern would most definitely be into yoga.

Family of algorithms

The Strategy pattern is meant to define families of algorithms and abstract them into an interface. The calling code never knows or cares what implementation its using.

If the Strategy pattern had a dating profile, it would definitely have a group pic where its impossible to tell which one they are. Strategy Pattern Tinder Profile

Common

Being basic gets a bad wrap. However when it comes to design patterns, being common is not a bad thing. The Strategy pattern is one of the most used patterns I’ve seen in the real world and solves many common development problems. I always recommend it as the first pattern that people learn, because I think its the pattern with the best shelf life. It’s flexible, readable, and accessible for both junior and senior coders alike.

Strategy Pattern Tinder Profile

Well-Rounded Dev

Liked this post? Subscribe to receive semi-regular thoughts by email.

    I won't send you spam. Unsubscribe at any time.