Member-only story
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!
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
public interface ILogger
{
void Log(string message);
}
That’s our interface. There’s not much to the contract. We have a method called Log
which expects a string
message.