Singleton Pattern Definition & Meaning

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.

Creational

  • Abstract factory
  • Builder
  • Factory
  • Prototype
  • Singleton

Structural

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

Behavioral

  • 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
     }
};

 

Related Links

Abby Dykes
Abby Dykes
Abby Dykes is a writer and editor for websites such as TechnologyAdvice.com, Webopedia.com, and Project-Management.com, 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

The Complete List of 1500+ Common Text Abbreviations & Acronyms

UPDATED: This article was updated April 6, 2021 by Web Webster   From A3 to ZZZ we list 1,559 text message and online chat abbreviations to...

How to Create a Website Shortcut on Your Desktop

UPDATED: This article was updated April 6, 2021 by Web Webster   This Webopedia guide will show you how to create a desktop shortcut to a...

Windows Operating System History & Versions

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

What are the 5 Generations of Computers?

UPDATED: This article was updated on April 6, 2021 by Web Webster   Learn about each of the 5 generations of computers and major technology developments...

Random Access Memory (RAM)...

UPDATED: This article Updated April 6, 2021 by Web Webster   Random Access Memory (RAM)...

OEM – original equipment...

UPDATED: This article was updated April 6, 2021 by Web Webster OEM (pronounced as...

Best ERP Software for...

UPDATED: This page was updated April 6, 2021 by Web Webster   Enterprise resource planning...