liveBook · Manning

Singleton pattern

As explained in Object-Oriented Software Design in C++

The Singleton design pattern is a creational design pattern that restricts a class to one instance and provides a controlled access point to it.

The Singleton pattern models a class for which an application may have at most one object. It also provides a controlled access point to that object. A printer spooler or a single stadium executive pass illustrates the requirement: every request reaches the same object. section 14.0 section 14.1

A sound singleton design must:

  • Provide an access operation that always returns the same object. section 14.1.1
  • Create the object automatically when first requested, rather than requiring the application to create it. section 14.1.1
  • Avoid creating the object when it is never requested. section 14.1.1
  • Prevent copying so assignment and the copy constructor cannot produce additional instances. section 14.1.3

Refactoring ExecutivePass

A global ExecutivePass seems simple, but callers can copy it, producing independent passes with different keys. Global state also permits unexpected changes, creates order-of-initialization problems, and keeps the pass alive even when unused. Refactor it by making the constructor private, deleting the copy constructor and copy assignment operator, and storing the sole pointer in a private static member. A public static obtain(holder) function creates the object when the pointer is nullptr, updates its holder, and returns it. Deleting the object resets the pointer, allowing later creation. Ron and Sal therefore receive the same key; after deletion, Bob and Flo share a newly created key. section 14.1.2 section 14.1.3

The generic structure replaces ExecutivePass with Singleton, single_instance with singleton, obtain() with get_singleton(), and application-specific data and operations with generic members. The pattern controls construction and copying, but its check-then-create operation needs extra care in multithreaded programs: simultaneous callers can create multiple objects. section 14.1.4 section 14.4

Figure 14.2 A well-designed singleton object. The constructor is private. The copy constructor and the copy assignment operator are both disabled. The static private member variable single_instance points to the singleton object, and the static public member function obtain() returns the value of the pointer.

Use the class diagram to see how the private constructor, disabled copy operations, static instance pointer, and public access function enforce the design. section 14.1.3

How Object-Oriented Software Design in C++ covers it

How deep it goes
Explains construction, copying, lazy creation, deletion, and multithreading risks, but defers race-condition solutions.
Examples
Demonstrates printer spooling, a stadium executive pass, global copies, and repeated obtain-delete-obtain calls.
What it assumes
Assumes basic classes, constructors, pointers, static members, and C++ copy operations; teaches singleton structure through refactoring.
What you can do after
Build a C++ singleton with private construction, disabled copying, lazy creation, static access, and deletion-aware reuse.
How it connects
Connects singleton objects with global variables, lazy construction, copy constructors, copy assignment, multithreading, and race conditions.

Also explained in

Compare how 6 books explain Singleton pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.