O'Reilly logo

Modern C++ Design: Generic Programming and Design Patterns Applied by Andrei Alexandrescu

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

6. Implementing Singletons

The Singleton design pattern (Gamma et al. 1995) is unique in that it’s a strange combination: its description is simple, yet its implementation issues are complicated. This is proven by the abundance of Singleton discussions and implementations in books and magazine articles (e.g., Vlissides 1996, 1998). Singleton’s description in the GoF book is as simple as it gets: “Ensure a class only has one instance, and provide a global point of access to it.”

A singleton is an improved global variable. The improvement that Singleton brings is that you cannot create a secondary object of the singleton’s type. Therefore, you should use Singleton when you model types that conceptually have a unique instance in the application, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required