Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Singleton Pattern | Creational Patterns
JavaScript Design Patterns

bookSingleton Pattern

The Singleton pattern is a creational design pattern that restricts the instantiation of a class to one single object. In JavaScript, this pattern is especially useful when you need to coordinate actions across the system using a single shared resource. Common use cases include managing a single database connection; handling configuration settings; or maintaining a single logging instance. By ensuring that only one instance exists, you avoid issues such as conflicting states or unnecessary resource consumption.

1234567891011121314151617181920212223242526
// Simple Singleton implementation using an object const Singleton = (function() { let instance; function createInstance() { return { message: "I am the only instance!" }; } return { getInstance: function() { if (!instance) { instance = createInstance(); } return instance; } }; })(); // Usage const singletonA = Singleton.getInstance(); const singletonB = Singleton.getInstance(); console.log(singletonA === singletonB); // true console.log(singletonA.message); // "I am the only instance!"
copy

This Singleton implementation works by using a closure to keep the instance variable private. When you call getInstance(), it checks if an instance already exists. If not, it creates one and stores it. Any subsequent calls to getInstance() will return the same object. This ensures that throughout your code, there is only one instance of the Singleton, and any changes to it are shared everywhere it is accessed.

1. What is the main purpose of the Singleton pattern?

2. In JavaScript, a Singleton is often implemented using:

question mark

What is the main purpose of the Singleton pattern?

Select the correct answer

question mark

In JavaScript, a Singleton is often implemented using:

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 7.14

bookSingleton Pattern

Sveip for å vise menyen

The Singleton pattern is a creational design pattern that restricts the instantiation of a class to one single object. In JavaScript, this pattern is especially useful when you need to coordinate actions across the system using a single shared resource. Common use cases include managing a single database connection; handling configuration settings; or maintaining a single logging instance. By ensuring that only one instance exists, you avoid issues such as conflicting states or unnecessary resource consumption.

1234567891011121314151617181920212223242526
// Simple Singleton implementation using an object const Singleton = (function() { let instance; function createInstance() { return { message: "I am the only instance!" }; } return { getInstance: function() { if (!instance) { instance = createInstance(); } return instance; } }; })(); // Usage const singletonA = Singleton.getInstance(); const singletonB = Singleton.getInstance(); console.log(singletonA === singletonB); // true console.log(singletonA.message); // "I am the only instance!"
copy

This Singleton implementation works by using a closure to keep the instance variable private. When you call getInstance(), it checks if an instance already exists. If not, it creates one and stores it. Any subsequent calls to getInstance() will return the same object. This ensures that throughout your code, there is only one instance of the Singleton, and any changes to it are shared everywhere it is accessed.

1. What is the main purpose of the Singleton pattern?

2. In JavaScript, a Singleton is often implemented using:

question mark

What is the main purpose of the Singleton pattern?

Select the correct answer

question mark

In JavaScript, a Singleton is often implemented using:

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 1
some-alt