Implementing the Singleton pattern
The Singleton pattern will be used to ensure that a given class may only instantiate a single instance of that class. However, a true Singleton pattern will also have expansion capabilities to allow for multiple (but a well-defined number of) instances to be made. This unusual and not well-known caveat of the Singleton pattern is rare.
We will start with two simple Singleton implementations to understand their limitations. We will then progress to the more robust paired-class implementation of the Singleton, with the most common pattern goal of only allowing one Target class instantiation at any given time.
Using a simple implementation
To implement a very simple Singleton, we will use a straightforward single class specification for the Singleton itself. We will define a class, known as Singleton
, to encapsulate the pattern. We will ensure that our constructor(s) are private so that they cannot be applied more than once. We will also add...