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, ...

Get Modern C++ Design: Generic Programming and Design Patterns Applied now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.