Cross-Fading Transitions with the Service Locator Pattern
In games, many classes use a single class to perform common procedures. This is known as a service class. For instance, in Godot Engine, we have the Input
class, a singleton that offers services related to input that any class can access. Another example is the Engine
class, which provides data about the very engine itself to any class interested in knowing it; the Engine
class is also a singleton. When we have singletons that provide services, especially if these services are provided through layers of indirection to other classes, we call them service locators. Well, this is not precisely what a service locator is, but it’s a way of understanding its purpose.
In this chapter, we will implement a common case for implementing the Service Locator pattern: audio, specifically, music. There is no reason to implement multiple instances of a class that plays music. This is undesirable, as multiple songs overlapping each...