Spotting use cases of singletons in games
Now that you understand what a singleton is, let’s research some real-world use cases that are very likely to happen in any game project. In these use cases, we will note that they will present a problem that we can solve by applying the Singleton pattern. As we saw, the problem that the Singleton pattern solves is that you require all classes to access the same instance of a given class. We do that by turning this instance globally accessible and ensuring that its reference will always point out to the same instance.
I want to make sure that you understand this; the Singleton pattern is not about having a static
method that returns an instance of a class and prevents the creation of other instances. This is how programmers typically implement it.
However, the goal of the Singleton pattern itself is to provide a globally accessible instance of a class that all other classes can access. The reason why we typically prevent other...