79: Design Patterns: Strategy.

Take Up Code - En podcast af Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Kategorier:

What is the strategy design pattern? The strategy behavioral pattern allows you to define multiple ways to perform some action and then select the best approach. This pattern has some similarities to the previous state design pattern. Both patterns describe how to change behavior. But the state design pattern is more focused on changing the behavior when some internal state changes causing the program to go into different modes. While the strategy pattern is more about exposing choices that can be selected based on what’s best. A good example of this pattern is when an adventure game allows users to select their desired difficulty level before starting a new game. Each difficulty level performs the same basic task of determining how many and how powerful the various opponents will be. The rest of the game doesn’t need to worry about which difficulty strategy was selected. And each strategy is interchangeable because the user can select which one to use. Some games even let the user change the difficulty level after the game has started. You could think of something like this as a state, especially for these user controlled strategies, but the overall idea is different enough to warrant its own design pattern. You probably wouldn’t think of changing the difficulty level as if it was a mode that can be changed like a drawing program would change the current tool in use or like how the adventure hero gets in and out of a boat. A difficulty level is more pervasive and controls how things are done throughout the game. It forms a broader strategy. If you’d like to read the book that describes this pattern along with diagrams and sample code, then you can find Design Patterns at the Resources page. You can find all my favorite books and resources at this page to help you create better software designs. Listen to the full episode or you can also read the full transcript below. Transcript The basic description says that this pattern defines a family of algorithms that are all encapsulated and interchangeable. It then lets the algorithm change independently of the client that uses it. This pattern has some similarities to the previous pattern, state. But there are differences. Both patterns describe how to change behavior. But the state design pattern is more focused on changing the behavior when some internal state changes causing the program to go into different modes. The strategy pattern is more about exposing choices that can be selected based on what’s best. These choices are not usually something that’s available directly to the end user but they could be. A good example of this pattern that’s usually made available to the user is when a game allows users to select their desired difficulty level before starting a new game. Each difficulty level performs the same basic task of determining how many and how powerful the various opponents will be. This is what the description means when it says the strategies are encapsulated. The rest of the game doesn’t need to worry about which difficulty strategy was selected. And each one is interchangeable because the user can select which one to use. Some games even let the user change the difficulty level after the game has started. You could think of something like this as a state especially for these user controlled strategies but the overall idea is different enough to warrant its own design pattern. To me this pattern really shines when it’s made available for the developers to use. What do I mean by this? Well, let’s examine various monsters or creatures in an adventure game. A small rat can move around and attack the hero. So can a troll. Sure, they’re going to have different health levels and inflict different amounts of damage but they’re also going to behave differently. Maybe the rat prefers to run away and will fight only when surprised or cornered. And maybe a troll lays claim to a certain piece of land and w

Visit the podcast's native language site