A GUIDE TO SOFTWARE DESIGN PATTERNS

Behavioral Design Pattern: Strategy

Peterson C
The Startup
3 min readMar 26, 2020

--

Photo by JESHOOTS.COM on Unsplash

The strategy pattern enables algorithms to switch at runtime. In plain English, that simply means deciding at runtime which concrete implementation of an interface to use using a context, which is where the actual concrete implementation injection happens. Deferring the decision on which algorithm to use until runtime allows more flexibility, reusability. It also makes the codebase more maintainable

This pattern comes in handy when multiple algorithms for a given strategy (interface) is needed. including the client, implementation of this pattern requires four components:

  1. Client -> where the context gets used
  2. Context -> where an algorithm is selected at runtime
  3. Strategy -> interface
  4. Algorithms -> concrete implementation

I’ve always found it easier to grasp new concepts using real-world scenarios. Let’s use one to see how we can how we implement it using this pattern.

Let’s say you’re part of a team that’s creating an application that does something cool. One of the must-have…

--

--

Peterson C
The Startup

Husband, father, engineer, musician, and writer by luck. I write mostly about whatever comes to mind. Follow me on this crazy writing journey if you dare.