Singleton Pattern

What Is the Singleton Pattern?

The Singleton pattern is used all the time, even if you don’t realize you’re using it.

For example, if you have some kind of class that keeps the total number of points in a game, you want only a single instance of that total. It doesn’t make any sense to have two totals where the game records only a single total score, like you find in a single-player pinball game.

Likewise, if you create a music application, you want the application to play only one tune at a time, and so you want only a single instance of the class that actually plays the music.

In fact, most applications have at least some feature where you want to make sure that there’s only a single instance, and that’s where you’ll want to use the Singleton design pattern.

Post A Comment

Anti-Spam Quiz: