Using the singleton design pattern
The singleton design pattern is the most commonly used design pattern for games. Unfortunately, it is also the most overused and most incorrectly applied design pattern for games. There are a few advantages of the singleton design pattern, which we will discuss. However, it has a lot of serious consequences as well.
Getting ready
To work through this recipe, you will need a machine running Windows. You also need to have a working copy of Visual Studio installed on your Windows machine. No other prerequisites are required.
How to do it…
In this recipe, we will see how easy it is to create a singleton design pattern. We will also see the common pitfalls of this design pattern:
- Open Visual Studio.
- Create a new C++ project.
- Select a Win32 console application.
- Add a source file called
Source.cpp
. - Add the following lines of code to it:
#include <iostream> #include <conio.h> using namespace std; class PhysicsManager { private: static bool bCheckFlag...