What is a Singleton?

The Singleton pattern in a nutshell is where you have a class that you can access anywhere within your project, due to the fact that only one object (instance) of that class is created (instantiated). The pattern provides a way for programmers to give access to a class's information globally by creating a single instance of an object in your game.

Whereas there are quite a few issues with using global variables, you can think of a Singleton as an improved global variable due to the fact that you cannot create more than one. With this in mind, the Singleton pattern is an attractive choice for classes that only have a unique instance in your game project, such as your graphics pipeline and input libraries, as having more than one of these in your projects doesn't make sense.

This single object uses a static variable and static functions to be able to access the object without having to pass it through all of our code.

In the Mach5 engine, Singletons are used for the application's, input, graphics, and physics engines. They are also used for the resource manager, object manager, and the game state manager. We will be taking a much closer look at one of the more foundational ones in the engine, the Application class, later on in this chapter. But before we get to it, let's dive into how we can actually create one of our very own.

There are multiple ways to implement the Singleton pattern or to get Singleton-like behavior. We'll go over some of the commonly seen versions and their pros and cons before moving to our final version, which is how the Mach5 engine uses it.

One very common way of implementing the functionality of the Singleton pattern would look something like the following:

Through code, it will look a little something like this:

class Singleton 
{
public:
static Singleton * GetInstance()
{
// If the instance does not exist, create one
if (!instance)
{
instance = new Singleton;
}

return instance;
}

private:
static Singleton * instance;
};

In this class, we have a function called GetInstance and a single property called instance. Note that we are using pointers in this instance, and only allocating memory to create our Singleton if we are actually using it. The instance property represents the one and only version of our class, hence it being made static. As it is private though, there is no way for others to access its data unless we give them access to it. In order to give this access, we created the GetInstance function. This function will first check whether instance exists and if it doesn't yet, it will dynamically allocate the memory to create one, set instance to it, and then return the object.

This will only work if instance is properly set to 0 or nullptr when initialized, which thankfully is the default behavior of static pointers in C++.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.50.87