Singleton Pattern

A singleton is a software design pattern that restricts the instantiation of a class to one single instance. Instantiation is the process of deriving an individual statement from a general one by replacing the variable with a name or other referring expression. In other words, a singleton is a class that allows a single instance of itself to be created, and it gives access to that created instance. This design pattern is useful when exactly one object is needed to coordinate actions across an entire software system. It’s used in programming languages such as Java and .NET.

What does the singleton pattern do?

The singleton pattern is one of 23 well-known design patterns (detailed below) that describe how to solve recurring problems in order to create objects that are easier to implement, change, test, and reuse. The key idea behind this pattern is to make the class itself responsible for controlling its instantiation and to ensure that it’s only instantiated once. The singleton design pattern works to answer questions such as:

  • How can it be guaranteed that a class only has one instance?
  • How can the single instance be easily accessed?
  • How can a class control its instantiation?
  • How can the number of instances be restricted?
  • How can a global variable be accessed?

Design patterns

Design Patterns: Elements of Reusable Object-Oriented Software is a software engineering book that’s considered an important source for object-oriented design theory and practice. In it, 23 classic software design patterns are described under three umbrella categories: Creational, Structural, and Behavioral. The 23 design patterns are detailed below.


  • Abstract factory
  • Builder
  • Factory
  • Prototype
  • Singleton


  • Adapter
  • Bridge
  • Composite
  • Decorator
  • Facade
  • Flyweight
  • Proxy


  • Chain of responsibility
  • Command
  • Interpreter
  • Iterator
  • Mediator
  • Memento
  • Observer
  • State
  • Strategy
  • Template
  • Visitor

Singleton pattern use cases

The singleton design pattern is a part of the creational patterns, which create objects rather than having to instantiate objects directly. This gives a program more flexibility in deciding which objects need to be created. Singletons are sometimes considered to be an alternative to global variables or static classes.

A global variable is a variable with global scope, meaning it’s visible throughout the program unless shadowed. They are created outside of a function and can be used by everyone, whether inside and outside of functions. The singleton design is often preferred to global variables because:

  • Singleton instance fields don’t take up space in the global namespace
  • Singletons permit lazy allocation and initialization whereas global variables will consume resources

A static class is a class that cannot be instantiated. A static class is similar to a class that is both abstract and sealed, and all members of the class are static in nature. The singleton design is preferred over static class because:

  • Singletons can implement interfaces
  • Singletons can be passed as parameters
  • Singletons can have instances swapped out
  • Singletons can be handled polymorphically, meaning multiple implementations can exist.

Implementing the Singleton pattern in JavaScript

Users can use the following object literal below to implement the Singleton design pattern in JavaScript:

var singleton = {
  method1: function () {
    //Write your usual code here
     method2: function () {
     //Write your usual code here


Abby Braden
Abby Braden is an award-winning writer and editor for websites such as,, and, where she covers technology trends and enterprise and SMB project management platforms. When she’s not writing about technology, she enjoys giving too many treats to her dog and coaching part-time at her local gym.

Top Articles

List of Windows Operating System Versions & History [In Order]

The Windows operating system (Windows OS) refers to a family of operating systems developed by Microsoft Corporation. We look at the history of Windows...

How to Create a Website Shortcut on Your Desktop

Website Shortcut on Your Desktop reviewed by Web Webster   This Webopedia guide will show you how to create a website shortcut on your desktop using...

What are the Five Generations of Computers? (1st to 5th)

Reviewed by Web Webster Learn about each of the 5 generations of computers and major technology developments that have led to the computing devices that...

Hotmail [Outlook] Email Accounts

Launched in 1996, Hotmail was one of the first public webmail services that could be accessed from any web browser. At its peak in...

Indicators of Compromise

When a system administrator finds anomalous or malicious behavior within network...

Disk Drive

A disk drive is a device that allows a computer to read from...

Firewall as a Service...

For the cloud-first organization, Firewall as a Service (FWaaS) brings all...