Today I’ll be going through the Strategy Design Pattern. However, before we go in-depth, let's start off with a scenario that may seem all too common to some of us (especially when we become lazy with our code).
What if we wanted to create an application to perform some sort of calculation? Well we could create a method to handle the calculations! But these calculations could vary depending on the choice of the user or the input of the function. How should we handle it? Should we do something like this?
// Assuming only 1 of the inputs are true
“All problems in computer science can be solved by another level of indirection” Butler Lampson
Well… not all I bet but probably a lot of them! What does ‘indirection’ here mean? It actually means the act of referencing something (an object etc) instead of the actual value itself. How does it come into play here? —While not the same meaning of simply setting up an abstraction layer, the key idea is there. Simply put, it may benefit us to set up some sort of intermediary reference instead of directly interacting with an object. In this case, an abstraction layer.