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.

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

 

Abby Braden
Abby Braden
Abby Braden is an award-winning 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.
Get the Free Newsletter
Subscribe to Daily Tech Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter
Subscribe to Daily Tech Insider for top news, trends & analysis
This email address is invalid.

Related Articles

Embedded Analytics

Embedded analytics brings self-service business intelligence to everyday application users.

HRIS

Human resources information system (HRIS) solutions help businesses manage multiple facets of their workforce operations. They provide a central platform for human resources professionals...

Complete List of Cybersecurity Acronyms

Cybersecurity news and best practices are full of acronyms and abbreviations. Without understanding what each one means, it's difficult to comprehend the significance of...

Human Resources Management System

A Human Resources Management System (HRMS) is a software application that supports many functions of a company's Human Resources department, including benefits administration, payroll,...

ScalaHosting

ScalaHosting is a leading managed hosting provider that offers secure, scalable, and affordable...

HRIS

Human resources information system (HRIS) solutions help businesses manage multiple facets of their...

Best Managed Service Providers...

In today's business world, managed services are more critical than ever. They can...