Managing Access with the Singleton Pattern
In the last chapter, we went over the core of what design patterns are, the common problems they solve, and how we’ll go about learning and implementing each of them throughout our adventure. In this chapter, we’ll start our practical journey by exploring the Singleton pattern, which helps when you want a single instance of a class to be globally accessible. For applications and games, you’ll commonly see this type of functionality with manager or service classes that keep track of global state or provide access to system-wide utilities. However, we need to be aware of potential risks with global state (and how to protect our newly accessible data), which we’ll discuss later in the chapter.
Anytime you bring up the Singleton pattern in programming circles, you’re likely to hear an audible sigh, some hushed booing, and maybe even an angry shout or two. And that’s precisely why I like to teach this...