Skip to main content

Singleton Design Pattern

Creational: Singleton Pattern


Could you please explain the singleton pattern?

View Answer:
Interview Response: The Singleton pattern is a design principle restricting a class's instantiation to one object. That's also useful when just one object is required to coordinate system-wide actions. The Singleton pattern traditionally gets implemented by creating a class with a method that creates a new class instance even if one doesn't already exist. If an object's instance already exists, it simply returns a pointer to it.

Diagram:



The objects participating in this pattern are:

Singleton -- In example code: MySingleton

  • It returns an instance via a constructor.
  • In charge of creating and managing the instance object.

Code Example:

// ES2015+ keywords/syntax used: const, let, arrow function syntax
// class, constructor, import, export

// Instance stores a reference to the Singleton
let instance;

// Private methods and variables
const privateMethod = () => {
console.log('I am private');
};
const privateVariable = 'Im also private';
const randomNumber = Math.random();

// Singleton
class MySingleton {
// Get the Singleton instance if one exists
// or create one if it doesn't
constructor() {
if (!instance) {
// Public property
this.publicProperty = 'I am also public';
instance = this;
}

return instance;
}

// Public methods
publicMethod() {
console.log('The public can see me!');
}

getRandomNumber() {
return randomNumber;
}
}
// [ES2015+] Default export module, without name
export default MySingleton;


// Instance stores a reference to the Singleton
let instance;

// Singleton
class MyBadSingleton {
// Always create a new Singleton instance
constructor() {
this.randomNumber = Math.random();
instance = this;

return instance;
}

getRandomNumber() {
return this.randomNumber;
}
}

export default MyBadSingleton;

// Usage:
import MySingleton from './MySingleton';
import MyBadSingleton from './MyBadSingleton';

const singleA = new MySingleton();
const singleB = new MySingleton();

console.log(singleA.getRandomNumber() === singleB.getRandomNumber()); // true

const badSingleA = new MyBadSingleton();
const badSingleB = new MyBadSingleton();

console.log(badSingleA.getRandomNumber() !== badSingleB.getRandomNumber()); // true

// Note: as we are working with random numbers, there is a
// mathematical possibility both numbers will be the same,
// however unlikely. The above example should otherwise still
// be valid.

What pattern family does the Singleton belong to?

View Answer:
Interview Response: The singleton pattern is a type of Creational design pattern.

What distinguishes Singletons from static classes or objects?

View Answer:
Interview Response: Singletons vary from static classes (or objects). Their initialization delays, typically because they require information that may not be available at the time of initialization. They don't make it easy for code that isn't aware of a previous reference to them to find them. A Singleton returns a structure rather than an object or a "class." Consider how closure variables aren't closures - the closure is the function scope that provides the closure.

What are some of the advantages of using the Singleton Pattern?

View Answer:
Interview Response: Benefits of the Singleton Pattern

  • You can be certain that a class only has one instance.
  • You are granted global access to that instance.
  • The singleton object only gets initialized the first time it is requested.


What are some of the Singleton Pattern's drawbacks?

View Answer:
Interview Response: Restricting the instantiation to just one instance could save a lot of memory space. Instead of setting up memory for a new instance each time, we only have to set up memory for that one instance referenced throughout the application. However, Singletons are considered an anti-pattern, and we should try to avoid using them in JavaScript.

  • Infringes on the Single Responsibility Principle: At the same time, the pattern solves two problems.
  • The Singleton pattern can hide lousy design, such as when application components know too much about each other.
  • In a multithreaded environment, the pattern gets treated differently so that multiple threads do not create a singleton object multiple times.
  • Unit testing the Singleton's client code may be complicated because many test frameworks rely on inheritance when producing mock objects. This reliance is relative to the constructor of the singleton class being private, and overriding static methods is impossible in most languages. You'll need to develop a unique way to mock the Singleton. Or don't write the tests at all. Alternatively, avoid using the Singleton pattern.