Singleton pattern implementation in JavaScript
This post will discuss possible ways to implement the singleton pattern in JavaScript.
The singleton pattern is a design pattern that ensures that only one instance of a class or an object exists in the application. It can be useful for managing global state, sharing resources, or implementing caching, or coordinating system-wide actions. There are different ways to implement the singleton pattern in JavaScript, but one common function is to use an immediately invoked function expression (IIFE) that returns an object with a getInstance() function. The getInstance() function checks if an instance already exists; if not, it creates one and stores it in a private variable. Then, it returns the instance to the caller.
1. Using a function expression
One way is to use a function expression and an immediately invoked function expression (IIFE) to create a private scope and return the singleton instance. This way, we can use closures to encapsulate the instance and prevent it from being modified or accessed from outside. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
let singleton = (function() { // Declare the instance variable let instance; // Define the constructor function function init() { // Private properties and functions let privateVar = "I am private"; let privateFunction = function() { console.log(privateVar); }; // Public properties and functions return { publicVar: "I am public", publicFunction: function() { privateFunction(); }, }; } // Return the singleton instance return { getInstance: function() { if (!instance) { instance = init(); } return instance; }, }; })(); // Test the singleton let instance1 = singleton.getInstance(); let instance2 = singleton.getInstance(); console.log(instance1 === instance2); // true instance1.publicFunction(); // I am private |
2. Using a class expression
Another way is to use a class expression and a static property to store and access the singleton instance. This way, we can use the class syntax to define the constructor and the functions of the singleton, and use the static keyword to ensure that only one instance is created and shared. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
let singleton = class { // Declare the static property static instance; // Define the constructor function constructor() { // Check if the instance already exists if (singleton.instance) { return singleton.instance; } // Assign the instance to the static property singleton.instance = this; // Private properties and functions this.privateVar = "I am private"; this.privateFunction = function() { console.log(this.privateVar); }; // Public properties and functions this.publicVar = "I am public"; this.publicFunction = function() { this.privateFunction(); }; } }; // Test the singleton let instance1 = new singleton(); let instance2 = new singleton(); console.log(instance1 === instance2); // true instance1.publicFunction(); // I am private |
3. Using an object literal
A third way is to use an object literal and the Object.freeze() function to create and protect the singleton instance. This way, we can use a simple object notation to define the properties and functions of the singleton, and use the Object.freeze() function to prevent any modification or addition of new properties. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
let singleton = { // Private properties and functions privateVar: "I am private", privateFunction: function() { console.log(this.privateVar); }, // Public properties and functions publicVar: "I am public", publicFunction: function() { this.privateFunction(); }, }; // Freeze the object Object.freeze(singleton); // Test the singleton let instance1 = singleton; let instance2 = singleton; console.log(instance1 === instance2); // true instance1.publicFunction(); // I am private |
That’s all about singleton pattern implementation in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)