Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it
Let's implement an example to demonstrate the usefulness of this pattern. We have mobile platform that wants to use different rendering strategy offers by various library like React, Vue and Angular. First we want to define a strategy interface as below
Next we want to implement this interface by coding up several libraries mentioned above
And now the context that contains a strategy
Finally, we have the Strategy Pattern implemented in Go. With this pattern in place, we not only provide a strong loosely coupled code but also flexibility of the application. Take for instance a OAuth strategy, we can have various strategies developed independently and use it at run time such as Google, Facebook, Github, et cetera
P/s: The accompany code is included in this PR 😊
Happy coding 😎