JS Singleton Pattern

beginner
17 min

JS Singleton Pattern

Welcome to our deep dive into the world of JavaScript (JS)! Today, we're going to explore the Singleton Pattern, a design pattern that ensures a class has only one instance, and provides a global point of access to it. Let's get started! 🎯

Understanding the Singleton Pattern

In simple terms, the Singleton Pattern is a design pattern that restricts the instantiation of a class and ensures that only one instance of the class exists in the entire application. This pattern is particularly useful when you want to control access to a resource or ensure that a class is only used in one place. 📝

Why Use the Singleton Pattern?

  1. Resource Management: The Singleton Pattern can be used to manage global resources, such as a database connection or a logger. By ensuring only one instance exists, you avoid conflicts and simplify resource management.

  2. Lazy Initialization: Singletons can be instantiated only when needed, also known as lazy initialization. This can help optimize memory usage and improve performance in large applications.

Creating a JS Singleton

Now, let's see how to create a Singleton in JavaScript. We'll start with a simple example and then move on to a more advanced one.

Example 1 - The Basic Singleton

javascript
let Singleton = (function() { let instance; function createInstance() { const obj = new Object(); return obj; } return { getInstance: function() { if (!instance) { instance = createInstance(); } return instance; } }; })();

In this example, we're creating a Singleton object that guarantees only one instance will be created. The createInstance function is used to create the object, and getInstance is used to access the Singleton instance.

Example 2 - The Enhanced Singleton

While the previous example works, it's not ideal because it uses the new operator and doesn't check if the Singleton already exists before creating a new one. Here's a more enhanced version:

javascript
let Singleton = (function(_className) { let instance; function createInstance() { return new _className(); } return { getInstance: function() { if (!instance) { instance = createInstance(); } return instance; } }; })(function() { function SingletonMethods() { this.property = "I'm the only one!"; } SingletonMethods.prototype.getProperties = function() { return this.property; }; return SingletonMethods; });

In this example, we've created a Singleton with a constructor function and added some methods to it. This approach makes it easier to extend the Singleton with additional functionality.

Putting it into Practice

Now that you've learned about the Singleton Pattern, let's see how it can be used in a real-world scenario. Imagine a simple logging system for a web application. A Singleton can be used to ensure that only one logging instance exists, and all log messages are directed to this single instance.

Quiz Time 🎯

Quick Quiz
Question 1 of 1

What is the primary purpose of the Singleton Pattern?

That's it for today! We hope you enjoyed learning about the JS Singleton Pattern. Stay tuned for more in-depth tutorials on CodeYourCraft! 💡