From the course: C++ Design Patterns: Creational

The need for a design pattern

- [Instructor] One of the biggest challenges in software development, is creating something that's flexible, maintainable and extensible. Code will undergo many changes, major and minor throughout the development process. And even after being released, most modern applications will receive updates to improve features or the fixed newly discovered bugs. Things are always changing, let's see how this may play out in a production environment. Imagine I work at a video game company where I'm creating a new adventure game with a main character that wanders around looking for treasure and fighting off little zombie creatures along the way. Each level of the game has a different theme, and the evil creature found in each level is associated with that theme. The first level takes place in the prairies where the character needs to fend off zombie dogs. The second level is in the mountains with zombie eagles. The third in the rainforest with zombie monkeys, and the fourth is underwater was zombie fish. The evil creatures in my game have a lot of properties in common. So I'll have them all derived from the same base class. The difference between them is how they look on screen, but they all share the same attributes and methods. As a player wanders around a level, the game generates different numbers of creatures at different locations. It does this because I've specified the exact number of creatures to instantiate at different locations in the levels. One day, my boss comes by my desk and says that each level should have more creatures. And he wants me to have a version ready for the game testers right away. So I go through the code and increase the number of creatures that are generated at each location. But in my haste, I missed one location. A few minutes later, my upset boss comes back and demands that I fix the mistake. No big deal, the following day, my boss tells me that beta users mentioned that it would be cool if some of the creatures could throw fireballs. Sure, I'll update the base class to include a method to throw a fireball, but then the testers come back asking, why are there fireballs under water? That makes no sense. Oops, by adding throw fireball to the base class, all my other classes inherit that behavior. So, I'll add a property to disable fireballs if the level happens to be underwater. The next round of tests or feedback says that having fireballs and almost every level gets kind of boring, it needs to be switched up a bit and then have it ready ASAP deadlines are coming. Okay, I'll keep the fireballs and the prairies, but I'll add snowballs in the mountains, poison berries in the rainforest and spiny coral underwater. Again, in my haste, I forget to update some of the places in my code. And then an occasional misplaced creature is found throwing the wrong item. My boss is furious, "How can there be so many bugs?" There's got to be a better way to manage all this. I'm constantly updating code and multiple places and clearly missing some of the locations. Soon, the game will likely expand to have more levels, how am I going to keep things straight? And things get even more complicated when there are multiple developers working on the same code. This is where a design pattern can be really useful. At the end of the course, I'll come back to the scenario and see how design patterns can help rein in this chaos.

Contents