Strategy
The goal of the Strategy design pattern is to allow an object to alter its behavior at runtime.
Let’s recall the platformer game we were designing in Chapter 3, Understanding Structural Patterns, while discussing the Facade design pattern.
Canary Michael, who acts as a game designer in our small indie game development company, came up with a great idea. What if we were to give our hero an arsenal of weapons to protect us from those horrible carnivorous snails?
Weapons all shoot projectiles (you don’t want to get too close to those dangerous snails) in the direction our hero is facing:
enum class Direction {
LEFT, RIGHT
}
In Chapter 2, Working with Creational Patterns, when we explored the Prototype design pattern, we emphasized the preference for creating immutable data classes and, if necessary, changing the object’s state by making a copy using the copy constructor. However, for the purposes of this example, let’s assume...