Implementing AI with the Strategy Pattern
The Strategy pattern is the simplest yet most useful pattern we can learn. I consider the Strategy pattern to be the mother of all patterns and the concretion of the composition over inheritance principle.
By applying the Strategy pattern, we can decouple the algorithms from the user class, allowing other classes to mutate the algorithm they want to be executed in at runtime. In this chapter, we’ll note some similarities between the Strategy pattern and other patterns we’ve seen so far, especially the other behavioral design patterns.
With the Strategy pattern implemented, our code base will have a safe framework to extend. Similar to the Command and State patterns, it turns elements of our class into interchangeable objects that we can inject and mutate at runtime. With that, we can dynamically compose object behaviors depending on the events that happen in our game.
Using the Strategy pattern adds yet another layer...