Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre 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

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 1

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Awesome!

Completion rate improved to 7.14

bookSingleton Pattern

Glissez pour afficher le menu

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

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 1
some-alt