Understanding the Contrast: Singleton Design Pattern with Big S versus Small S

Understand different types of Singletons

Shashank Thakur
3 min readApr 13, 2023
Understanding the Contrast: Singleton Design Pattern with Big S versus Small S

The term “singleton” generally refers to a design pattern used in object-oriented programming, where a class is designed to have only one instance in the entire application. There are two types of singleton patterns: big S Singleton and small s Singleton.

Big S Singleton

Big S Singleton, also known as “strict Singleton,” is a design pattern where a class has only one instance that is globally accessible throughout the application. The instance is usually created at the beginning of the program and remains in memory until the end of the program’s execution. In other words, the big S Singleton ensures that there is only one instance of a class that is available for use throughout the entire application, and this instance is created only once. Let's take an example

class BigSingleton {
static let sharedInstance = BigSingleton()
private init() {}
}

let obj1 = BigSingleton.sharedInstance

In this example, the class BigSingleton is designed as a Big S Singleton. The single instance of the class is created as a static property, which is created when the class is first accessed. The init() method is declared private, which prevents the creation of additional instances of the class.

Small S Singleton

Small s Singleton, also known as “lazy Singleton,” is a design pattern where a class has only one instance, but it is created lazily, i.e., the instance is created only when it is required for the first time. This means that the instance is not created when the application starts, but it is created on-demand, the first time it is accessed. After that, the same instance is reused throughout the application. Let’s take an example

class SmallSingleton {
static var sharedInstance: SmallSingleton {
struct Singleton {
static let instance = SmallSingleton()
}
return Singleton.instance
}
private init() {}
}

let obj1 = SmallSingleton.sharedInstance

In this example, the class SmallSingleton is designed as a Small s Singleton. The single instance of the class is created lazily when the sharedInstance property is first accessed. The instance is created using a private nested struct, which ensures thread safety and lazy instantiation. The init() method is declared private, which prevents the creation of additional instances of the class.

Difference

The main difference between big S and small s Singleton is in the way the instance of the singleton class is created. In the case of big S Singleton, the instance is created at the beginning of the program, while in the case of small s Singleton, the instance is created only when it is needed for the first time.

Both patterns have their own advantages and disadvantages, and the choice between them depends on the specific requirements of the application. For example, big S Singleton can ensure thread safety and prevent multiple instances of a class from being created, while small s Singleton can be more efficient in terms of memory usage, as the instance is created only when it is needed.

--

--