Software Engineering Practices: Strategy Pattern
Problem
Imagine building an application that automatically posts a cat photo. After spending an entire weekend building out the application, you are very proud of what you have done.
The code below shows the rough code of what is done.
Everyone loved the cat photos that you have posted. After your newfound success, many fans were asking you if you could do something similar with Dog photos.
Now, what do we do?
We can always make another function similar to postCatImage
and make it a dog version instead. The code below is an example.
However, as we add more different types of images, we will need to create more functions to accommodate different types of animals to be posted. This method does not scale well when we are looking at all the different types of animals in the animal kingdom.
So how else do we do this?
This is where the Strategy Design pattern comes in.
So what is the Strategy Design Pattern?
It is a design pattern that enables the selection of an algorithm during runtime, instead of implementing just a single algorithm.
Instead of implementing a particular algorithm, we store the different types of algorithms as a Strategy and select the strategies during runtime based on the input that is selected.
Advantages and Disadvantages
Pros:
- Algorithms can be swapped effectively at runtime
- The implementation of the algorithm is decoupled from the code that uses the algorithm
- We can introduce new strategies without having to modify the original code that calls the algorithm
Cons:
- Over-engineering when there are only a few algorithms
- The clients need to consider the differences between the strategies when calling the algorithm
How to effectively apply the Strategy Pattern?
We can apply the pattern by following the checklist below
- Identify a pivot point where the algorithm might differ
- Specify the interface for the algorithm
- Implement the different alternatives using the interface for the algorithm
- The users of the algorithm make use of the interface for implementation.
The solution to the example
In this case, the algorithm for generating the image as well as the credentials can be selected during runtime instead of being hardcoded into different functions.
We can instead do something similar to the one below.
In the code snippet above, the algorithm for image generation, the credentials, as well as the caption, are chosen during runtime.
During extension, the user can simply add another strategy by including the relevant functions within the dictionary. The function can simply be called with postImage
function with the appropriate typing.
Application in real life
Here is a non-exhaustive list of the application of Strategy Patterns in real life
- Making a burger
In this case, the strategy is to choose the different burger patties to use when a customer orders.
While the algorithm to prepare the burger patties might differ, the rest of the steps when making the burger remain similar.
- Encryption
For encryption, the plain text can be the same but based on the different encryption algorithms, the resulting ciphertext can be different.
In this case, the encryption algorithms are the Strategies.
Conclusions
While Strategy Pattern helps us organize our code when there is a need to swap in different algorithms, we must make sure to take note not to blindly apply it in every scenario.
Just because we added something new to our tool kit, it doesn’t mean that we should apply it everywhere we can. Do weigh the pros and cons of applying this pattern before using it.