- Game Development Patterns and Best Practices
- John P. Doran Matt Casanova
- 494字
- 2021-07-02 23:43:46
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 pe 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++.
- JavaScript從入門到精通(微視頻精編版)
- Java高并發(fā)核心編程(卷2):多線程、鎖、JMM、JUC、高并發(fā)設(shè)計(jì)模式
- OpenNI Cookbook
- 網(wǎng)頁設(shè)計(jì)與制作教程(HTML+CSS+JavaScript)(第2版)
- TypeScript實(shí)戰(zhàn)指南
- Rust Essentials(Second Edition)
- Android底層接口與驅(qū)動(dòng)開發(fā)技術(shù)詳解
- Multithreading in C# 5.0 Cookbook
- Visual Basic程序設(shè)計(jì)上機(jī)實(shí)驗(yàn)教程
- Node.js 12實(shí)戰(zhàn)
- Angular應(yīng)用程序開發(fā)指南
- C/C++代碼調(diào)試的藝術(shù)(第2版)
- 輕松學(xué)Scratch 3.0 少兒編程(全彩)
- Build Your Own PaaS with Docker
- MATLAB/Simulink建模與仿真