From the course: C++ Design Patterns: Creational

Unlock the full course today

Join today to access over 24,300 courses taught by industry experts.

Singleton concept

Singleton concept

- [Instructor] There are situations where an application requires an object that can be globally accessed, but requires one, and only one instance of this object. This is the perfect solution for a Singleton. You can think of a Singleton as being similar to the leader of a country. There can only ever be one at a given time. They're visible to everyone in their country and to the rest of the world. However, they can be replaced with a new leader after a vote, at which point the old leader is no longer referred to as the leader and the new replacement takes on the role. A simple programming example of this could be a global counter that is used in multiple areas of an application. At first, it can be tempting to just use a static global variable, which is also accessible anywhere in the application. But that's asking for trouble. For example, if the global variable is complex to instantiate, I would want to wait…

Contents