Understanding the Strategy pattern
To understand the Strategy pattern, we need to understand the main advantage of using the Object-oriented programming (OOP) paradigm. In Chapter 1, we learned that this paradigm presents a data structure called objects that bundles data and behavior together to allow its algorithm to have direct access to the data and mutate it according to its responsibilities. We also learned that one of OOP’s main features is inheritance, but instead of abusing it, we should use it purposefully and always favor composition over inheritance. By doing that, we decouple the object’s functionalities from its class, whereas by using inheritance, we always bind – or rather, couple – a subclass with its superclass, instead of turning them into individual classes that can be composed by having them become users of different other classes.
In Chapter 8, we learned how to turn the object’s states into interchangeable classes that we...