Design Patterns: Strategy
The strategy pattern is one of the first patterns I learned, and it leads into at least one other pattern that I used constantly! Let’s do this thing!
I’ve not been having as much time to focus on writing as I’d like, so I figured I’d try to knock out some pattern review for myself while hopefully working on more significant posts in the background. The first post in this potential series of posts - The strategy pattern!
The strategy pattern
In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of algorithms to use.
from Wikipedia
What does that mean? Well to me, it’s just having multiple implementations of a single interface. That’s it. One of the first things I think about when it comes to the strategy pattern, is having multiple “Provider” implementations for something like logging.
Implementation
1 |
|
That’s our interface. There’s not much to the contract. We have a method called Log
which expects a string
message.
So how does this get utilized as a “strategy”? Multiple implementations!
1 |
|
(Note, don’t use that file logger, it’s terrible… In fact! don’t use any of it, it’s just an example!)
1 |
|
Reasons to use this pattern
What does the use of this pattern buy us?
- Writing code to the abstraction that is
ILogger
rather than a concrete implementation - making Unit testing easier. - Select an algorithm implementation at run time, perhaps based on a user’s input, or configuration.
- Ties into other patterns, one of my favorites being the “Factory“.
Note that I got some pushback in a programming slack group that the above is not in fact an example of the strategy pattern, due to not having selected the implementation at run time (since I did it in code at compile time). I disagree, but wanted to throw that out there cuz I’ve been wrong before! :D
The code for this pattern can be found on my pattern repo: https://github.com/Kritner-Blogs/Kritner.PatternExamples
Resources:
Design Patterns: Strategy
https://blog.kritner.com/2020/02/11/design-patterns-strategy/