Using the observer pattern
The observer design pattern is one which is not commonly used in games, but it should be used more often by game developers as it is a very smart way to handle notifications. In the observer design pattern, a component maintains a one-to-many relationship with other components. This means when the main component changes, all the dependent components also update. Imagine a physics system. We want enemy1
and enemy2
to update as soon as the physics system updates, so we should use this pattern.
Getting ready
For this recipe, you will need a Windows machine with a working copy of Visual Studio.
How to do it…
In this recipe, we will find out how easy it is to implement the observer pattern:
Open Visual Studio.
Create a new C++ project.
Select a Win32 Windows application.
Add a source file called
Source.cpp
.Add the following lines of code to it:
#include <iostream> #include <vector> #include <conio.h> using namespace std; class PhysicsSystem { vector...